amd_iommu.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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/scatterlist.h>
  23. #include <linux/iommu-helper.h>
  24. #include <asm/proto.h>
  25. #include <asm/iommu.h>
  26. #include <asm/amd_iommu_types.h>
  27. #include <asm/amd_iommu.h>
  28. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  29. #define EXIT_LOOP_COUNT 10000000
  30. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  31. /* A list of preallocated protection domains */
  32. static LIST_HEAD(iommu_pd_list);
  33. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  34. /*
  35. * general struct to manage commands send to an IOMMU
  36. */
  37. struct iommu_cmd {
  38. u32 data[4];
  39. };
  40. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  41. struct unity_map_entry *e);
  42. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  43. static int iommu_has_npcache(struct amd_iommu *iommu)
  44. {
  45. return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
  46. }
  47. /****************************************************************************
  48. *
  49. * Interrupt handling functions
  50. *
  51. ****************************************************************************/
  52. static void iommu_print_event(void *__evt)
  53. {
  54. u32 *event = __evt;
  55. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  56. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  57. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  58. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  59. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  60. printk(KERN_ERR "AMD IOMMU: Event logged [");
  61. switch (type) {
  62. case EVENT_TYPE_ILL_DEV:
  63. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  64. "address=0x%016llx flags=0x%04x]\n",
  65. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  66. address, flags);
  67. break;
  68. case EVENT_TYPE_IO_FAULT:
  69. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  70. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  71. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  72. domid, address, flags);
  73. break;
  74. case EVENT_TYPE_DEV_TAB_ERR:
  75. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  76. "address=0x%016llx flags=0x%04x]\n",
  77. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  78. address, flags);
  79. break;
  80. case EVENT_TYPE_PAGE_TAB_ERR:
  81. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  82. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  83. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  84. domid, address, flags);
  85. break;
  86. case EVENT_TYPE_ILL_CMD:
  87. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  88. break;
  89. case EVENT_TYPE_CMD_HARD_ERR:
  90. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  91. "flags=0x%04x]\n", address, flags);
  92. break;
  93. case EVENT_TYPE_IOTLB_INV_TO:
  94. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  95. "address=0x%016llx]\n",
  96. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  97. address);
  98. break;
  99. case EVENT_TYPE_INV_DEV_REQ:
  100. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  101. "address=0x%016llx flags=0x%04x]\n",
  102. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  103. address, flags);
  104. break;
  105. default:
  106. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  107. }
  108. }
  109. static void iommu_poll_events(struct amd_iommu *iommu)
  110. {
  111. u32 head, tail;
  112. unsigned long flags;
  113. spin_lock_irqsave(&iommu->lock, flags);
  114. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  115. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  116. while (head != tail) {
  117. iommu_print_event(iommu->evt_buf + head);
  118. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  119. }
  120. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  121. spin_unlock_irqrestore(&iommu->lock, flags);
  122. }
  123. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  124. {
  125. struct amd_iommu *iommu;
  126. list_for_each_entry(iommu, &amd_iommu_list, list)
  127. iommu_poll_events(iommu);
  128. return IRQ_HANDLED;
  129. }
  130. /****************************************************************************
  131. *
  132. * IOMMU command queuing functions
  133. *
  134. ****************************************************************************/
  135. /*
  136. * Writes the command to the IOMMUs command buffer and informs the
  137. * hardware about the new command. Must be called with iommu->lock held.
  138. */
  139. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  140. {
  141. u32 tail, head;
  142. u8 *target;
  143. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  144. target = iommu->cmd_buf + tail;
  145. memcpy_toio(target, cmd, sizeof(*cmd));
  146. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  147. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  148. if (tail == head)
  149. return -ENOMEM;
  150. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  151. return 0;
  152. }
  153. /*
  154. * General queuing function for commands. Takes iommu->lock and calls
  155. * __iommu_queue_command().
  156. */
  157. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  158. {
  159. unsigned long flags;
  160. int ret;
  161. spin_lock_irqsave(&iommu->lock, flags);
  162. ret = __iommu_queue_command(iommu, cmd);
  163. spin_unlock_irqrestore(&iommu->lock, flags);
  164. return ret;
  165. }
  166. /*
  167. * This function is called whenever we need to ensure that the IOMMU has
  168. * completed execution of all commands we sent. It sends a
  169. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  170. * us about that by writing a value to a physical address we pass with
  171. * the command.
  172. */
  173. static int iommu_completion_wait(struct amd_iommu *iommu)
  174. {
  175. int ret = 0, ready = 0;
  176. unsigned status = 0;
  177. struct iommu_cmd cmd;
  178. unsigned long flags, i = 0;
  179. memset(&cmd, 0, sizeof(cmd));
  180. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  181. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  182. iommu->need_sync = 0;
  183. spin_lock_irqsave(&iommu->lock, flags);
  184. ret = __iommu_queue_command(iommu, &cmd);
  185. if (ret)
  186. goto out;
  187. while (!ready && (i < EXIT_LOOP_COUNT)) {
  188. ++i;
  189. /* wait for the bit to become one */
  190. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  191. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  192. }
  193. /* set bit back to zero */
  194. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  195. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  196. if (unlikely((i == EXIT_LOOP_COUNT) && printk_ratelimit()))
  197. printk(KERN_WARNING "AMD IOMMU: Completion wait loop failed\n");
  198. out:
  199. spin_unlock_irqrestore(&iommu->lock, flags);
  200. return 0;
  201. }
  202. /*
  203. * Command send function for invalidating a device table entry
  204. */
  205. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  206. {
  207. struct iommu_cmd cmd;
  208. int ret;
  209. BUG_ON(iommu == NULL);
  210. memset(&cmd, 0, sizeof(cmd));
  211. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  212. cmd.data[0] = devid;
  213. ret = iommu_queue_command(iommu, &cmd);
  214. iommu->need_sync = 1;
  215. return ret;
  216. }
  217. /*
  218. * Generic command send function for invalidaing TLB entries
  219. */
  220. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  221. u64 address, u16 domid, int pde, int s)
  222. {
  223. struct iommu_cmd cmd;
  224. int ret;
  225. memset(&cmd, 0, sizeof(cmd));
  226. address &= PAGE_MASK;
  227. CMD_SET_TYPE(&cmd, CMD_INV_IOMMU_PAGES);
  228. cmd.data[1] |= domid;
  229. cmd.data[2] = lower_32_bits(address);
  230. cmd.data[3] = upper_32_bits(address);
  231. if (s) /* size bit - we flush more than one 4kb page */
  232. cmd.data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  233. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  234. cmd.data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  235. ret = iommu_queue_command(iommu, &cmd);
  236. iommu->need_sync = 1;
  237. return ret;
  238. }
  239. /*
  240. * TLB invalidation function which is called from the mapping functions.
  241. * It invalidates a single PTE if the range to flush is within a single
  242. * page. Otherwise it flushes the whole TLB of the IOMMU.
  243. */
  244. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  245. u64 address, size_t size)
  246. {
  247. int s = 0;
  248. unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
  249. address &= PAGE_MASK;
  250. if (pages > 1) {
  251. /*
  252. * If we have to flush more than one page, flush all
  253. * TLB entries for this domain
  254. */
  255. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  256. s = 1;
  257. }
  258. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  259. return 0;
  260. }
  261. /* Flush the whole IO/TLB for a given protection domain */
  262. static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
  263. {
  264. u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  265. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
  266. }
  267. /****************************************************************************
  268. *
  269. * The functions below are used the create the page table mappings for
  270. * unity mapped regions.
  271. *
  272. ****************************************************************************/
  273. /*
  274. * Generic mapping functions. It maps a physical address into a DMA
  275. * address space. It allocates the page table pages if necessary.
  276. * In the future it can be extended to a generic mapping function
  277. * supporting all features of AMD IOMMU page tables like level skipping
  278. * and full 64 bit address spaces.
  279. */
  280. static int iommu_map(struct protection_domain *dom,
  281. unsigned long bus_addr,
  282. unsigned long phys_addr,
  283. int prot)
  284. {
  285. u64 __pte, *pte, *page;
  286. bus_addr = PAGE_ALIGN(bus_addr);
  287. phys_addr = PAGE_ALIGN(bus_addr);
  288. /* only support 512GB address spaces for now */
  289. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  290. return -EINVAL;
  291. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  292. if (!IOMMU_PTE_PRESENT(*pte)) {
  293. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  294. if (!page)
  295. return -ENOMEM;
  296. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  297. }
  298. pte = IOMMU_PTE_PAGE(*pte);
  299. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  300. if (!IOMMU_PTE_PRESENT(*pte)) {
  301. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  302. if (!page)
  303. return -ENOMEM;
  304. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  305. }
  306. pte = IOMMU_PTE_PAGE(*pte);
  307. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  308. if (IOMMU_PTE_PRESENT(*pte))
  309. return -EBUSY;
  310. __pte = phys_addr | IOMMU_PTE_P;
  311. if (prot & IOMMU_PROT_IR)
  312. __pte |= IOMMU_PTE_IR;
  313. if (prot & IOMMU_PROT_IW)
  314. __pte |= IOMMU_PTE_IW;
  315. *pte = __pte;
  316. return 0;
  317. }
  318. /*
  319. * This function checks if a specific unity mapping entry is needed for
  320. * this specific IOMMU.
  321. */
  322. static int iommu_for_unity_map(struct amd_iommu *iommu,
  323. struct unity_map_entry *entry)
  324. {
  325. u16 bdf, i;
  326. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  327. bdf = amd_iommu_alias_table[i];
  328. if (amd_iommu_rlookup_table[bdf] == iommu)
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. /*
  334. * Init the unity mappings for a specific IOMMU in the system
  335. *
  336. * Basically iterates over all unity mapping entries and applies them to
  337. * the default domain DMA of that IOMMU if necessary.
  338. */
  339. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  340. {
  341. struct unity_map_entry *entry;
  342. int ret;
  343. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  344. if (!iommu_for_unity_map(iommu, entry))
  345. continue;
  346. ret = dma_ops_unity_map(iommu->default_dom, entry);
  347. if (ret)
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. /*
  353. * This function actually applies the mapping to the page table of the
  354. * dma_ops domain.
  355. */
  356. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  357. struct unity_map_entry *e)
  358. {
  359. u64 addr;
  360. int ret;
  361. for (addr = e->address_start; addr < e->address_end;
  362. addr += PAGE_SIZE) {
  363. ret = iommu_map(&dma_dom->domain, addr, addr, e->prot);
  364. if (ret)
  365. return ret;
  366. /*
  367. * if unity mapping is in aperture range mark the page
  368. * as allocated in the aperture
  369. */
  370. if (addr < dma_dom->aperture_size)
  371. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  372. }
  373. return 0;
  374. }
  375. /*
  376. * Inits the unity mappings required for a specific device
  377. */
  378. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  379. u16 devid)
  380. {
  381. struct unity_map_entry *e;
  382. int ret;
  383. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  384. if (!(devid >= e->devid_start && devid <= e->devid_end))
  385. continue;
  386. ret = dma_ops_unity_map(dma_dom, e);
  387. if (ret)
  388. return ret;
  389. }
  390. return 0;
  391. }
  392. /****************************************************************************
  393. *
  394. * The next functions belong to the address allocator for the dma_ops
  395. * interface functions. They work like the allocators in the other IOMMU
  396. * drivers. Its basically a bitmap which marks the allocated pages in
  397. * the aperture. Maybe it could be enhanced in the future to a more
  398. * efficient allocator.
  399. *
  400. ****************************************************************************/
  401. /*
  402. * The address allocator core function.
  403. *
  404. * called with domain->lock held
  405. */
  406. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  407. struct dma_ops_domain *dom,
  408. unsigned int pages,
  409. unsigned long align_mask,
  410. u64 dma_mask)
  411. {
  412. unsigned long limit;
  413. unsigned long address;
  414. unsigned long boundary_size;
  415. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  416. PAGE_SIZE) >> PAGE_SHIFT;
  417. limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0,
  418. dma_mask >> PAGE_SHIFT);
  419. if (dom->next_bit >= limit) {
  420. dom->next_bit = 0;
  421. dom->need_flush = true;
  422. }
  423. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  424. 0 , boundary_size, align_mask);
  425. if (address == -1) {
  426. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  427. 0, boundary_size, align_mask);
  428. dom->need_flush = true;
  429. }
  430. if (likely(address != -1)) {
  431. dom->next_bit = address + pages;
  432. address <<= PAGE_SHIFT;
  433. } else
  434. address = bad_dma_address;
  435. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  436. return address;
  437. }
  438. /*
  439. * The address free function.
  440. *
  441. * called with domain->lock held
  442. */
  443. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  444. unsigned long address,
  445. unsigned int pages)
  446. {
  447. address >>= PAGE_SHIFT;
  448. iommu_area_free(dom->bitmap, address, pages);
  449. if (address + pages >= dom->next_bit)
  450. dom->need_flush = true;
  451. }
  452. /****************************************************************************
  453. *
  454. * The next functions belong to the domain allocation. A domain is
  455. * allocated for every IOMMU as the default domain. If device isolation
  456. * is enabled, every device get its own domain. The most important thing
  457. * about domains is the page table mapping the DMA address space they
  458. * contain.
  459. *
  460. ****************************************************************************/
  461. static u16 domain_id_alloc(void)
  462. {
  463. unsigned long flags;
  464. int id;
  465. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  466. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  467. BUG_ON(id == 0);
  468. if (id > 0 && id < MAX_DOMAIN_ID)
  469. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  470. else
  471. id = 0;
  472. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  473. return id;
  474. }
  475. /*
  476. * Used to reserve address ranges in the aperture (e.g. for exclusion
  477. * ranges.
  478. */
  479. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  480. unsigned long start_page,
  481. unsigned int pages)
  482. {
  483. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  484. if (start_page + pages > last_page)
  485. pages = last_page - start_page;
  486. iommu_area_reserve(dom->bitmap, start_page, pages);
  487. }
  488. static void dma_ops_free_pagetable(struct dma_ops_domain *dma_dom)
  489. {
  490. int i, j;
  491. u64 *p1, *p2, *p3;
  492. p1 = dma_dom->domain.pt_root;
  493. if (!p1)
  494. return;
  495. for (i = 0; i < 512; ++i) {
  496. if (!IOMMU_PTE_PRESENT(p1[i]))
  497. continue;
  498. p2 = IOMMU_PTE_PAGE(p1[i]);
  499. for (j = 0; j < 512; ++i) {
  500. if (!IOMMU_PTE_PRESENT(p2[j]))
  501. continue;
  502. p3 = IOMMU_PTE_PAGE(p2[j]);
  503. free_page((unsigned long)p3);
  504. }
  505. free_page((unsigned long)p2);
  506. }
  507. free_page((unsigned long)p1);
  508. }
  509. /*
  510. * Free a domain, only used if something went wrong in the
  511. * allocation path and we need to free an already allocated page table
  512. */
  513. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  514. {
  515. if (!dom)
  516. return;
  517. dma_ops_free_pagetable(dom);
  518. kfree(dom->pte_pages);
  519. kfree(dom->bitmap);
  520. kfree(dom);
  521. }
  522. /*
  523. * Allocates a new protection domain usable for the dma_ops functions.
  524. * It also intializes the page table and the address allocator data
  525. * structures required for the dma_ops interface
  526. */
  527. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  528. unsigned order)
  529. {
  530. struct dma_ops_domain *dma_dom;
  531. unsigned i, num_pte_pages;
  532. u64 *l2_pde;
  533. u64 address;
  534. /*
  535. * Currently the DMA aperture must be between 32 MB and 1GB in size
  536. */
  537. if ((order < 25) || (order > 30))
  538. return NULL;
  539. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  540. if (!dma_dom)
  541. return NULL;
  542. spin_lock_init(&dma_dom->domain.lock);
  543. dma_dom->domain.id = domain_id_alloc();
  544. if (dma_dom->domain.id == 0)
  545. goto free_dma_dom;
  546. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  547. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  548. dma_dom->domain.priv = dma_dom;
  549. if (!dma_dom->domain.pt_root)
  550. goto free_dma_dom;
  551. dma_dom->aperture_size = (1ULL << order);
  552. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  553. GFP_KERNEL);
  554. if (!dma_dom->bitmap)
  555. goto free_dma_dom;
  556. /*
  557. * mark the first page as allocated so we never return 0 as
  558. * a valid dma-address. So we can use 0 as error value
  559. */
  560. dma_dom->bitmap[0] = 1;
  561. dma_dom->next_bit = 0;
  562. dma_dom->need_flush = false;
  563. dma_dom->target_dev = 0xffff;
  564. /* Intialize the exclusion range if necessary */
  565. if (iommu->exclusion_start &&
  566. iommu->exclusion_start < dma_dom->aperture_size) {
  567. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  568. int pages = iommu_num_pages(iommu->exclusion_start,
  569. iommu->exclusion_length,
  570. PAGE_SIZE);
  571. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  572. }
  573. /*
  574. * At the last step, build the page tables so we don't need to
  575. * allocate page table pages in the dma_ops mapping/unmapping
  576. * path.
  577. */
  578. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  579. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  580. GFP_KERNEL);
  581. if (!dma_dom->pte_pages)
  582. goto free_dma_dom;
  583. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  584. if (l2_pde == NULL)
  585. goto free_dma_dom;
  586. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  587. for (i = 0; i < num_pte_pages; ++i) {
  588. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  589. if (!dma_dom->pte_pages[i])
  590. goto free_dma_dom;
  591. address = virt_to_phys(dma_dom->pte_pages[i]);
  592. l2_pde[i] = IOMMU_L1_PDE(address);
  593. }
  594. return dma_dom;
  595. free_dma_dom:
  596. dma_ops_domain_free(dma_dom);
  597. return NULL;
  598. }
  599. /*
  600. * Find out the protection domain structure for a given PCI device. This
  601. * will give us the pointer to the page table root for example.
  602. */
  603. static struct protection_domain *domain_for_device(u16 devid)
  604. {
  605. struct protection_domain *dom;
  606. unsigned long flags;
  607. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  608. dom = amd_iommu_pd_table[devid];
  609. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  610. return dom;
  611. }
  612. /*
  613. * If a device is not yet associated with a domain, this function does
  614. * assigns it visible for the hardware
  615. */
  616. static void set_device_domain(struct amd_iommu *iommu,
  617. struct protection_domain *domain,
  618. u16 devid)
  619. {
  620. unsigned long flags;
  621. u64 pte_root = virt_to_phys(domain->pt_root);
  622. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  623. << DEV_ENTRY_MODE_SHIFT;
  624. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  625. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  626. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  627. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  628. amd_iommu_dev_table[devid].data[2] = domain->id;
  629. amd_iommu_pd_table[devid] = domain;
  630. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  631. iommu_queue_inv_dev_entry(iommu, devid);
  632. iommu->need_sync = 1;
  633. }
  634. /*****************************************************************************
  635. *
  636. * The next functions belong to the dma_ops mapping/unmapping code.
  637. *
  638. *****************************************************************************/
  639. /*
  640. * This function checks if the driver got a valid device from the caller to
  641. * avoid dereferencing invalid pointers.
  642. */
  643. static bool check_device(struct device *dev)
  644. {
  645. if (!dev || !dev->dma_mask)
  646. return false;
  647. return true;
  648. }
  649. /*
  650. * In this function the list of preallocated protection domains is traversed to
  651. * find the domain for a specific device
  652. */
  653. static struct dma_ops_domain *find_protection_domain(u16 devid)
  654. {
  655. struct dma_ops_domain *entry, *ret = NULL;
  656. unsigned long flags;
  657. if (list_empty(&iommu_pd_list))
  658. return NULL;
  659. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  660. list_for_each_entry(entry, &iommu_pd_list, list) {
  661. if (entry->target_dev == devid) {
  662. ret = entry;
  663. list_del(&ret->list);
  664. break;
  665. }
  666. }
  667. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  668. return ret;
  669. }
  670. /*
  671. * In the dma_ops path we only have the struct device. This function
  672. * finds the corresponding IOMMU, the protection domain and the
  673. * requestor id for a given device.
  674. * If the device is not yet associated with a domain this is also done
  675. * in this function.
  676. */
  677. static int get_device_resources(struct device *dev,
  678. struct amd_iommu **iommu,
  679. struct protection_domain **domain,
  680. u16 *bdf)
  681. {
  682. struct dma_ops_domain *dma_dom;
  683. struct pci_dev *pcidev;
  684. u16 _bdf;
  685. *iommu = NULL;
  686. *domain = NULL;
  687. *bdf = 0xffff;
  688. if (dev->bus != &pci_bus_type)
  689. return 0;
  690. pcidev = to_pci_dev(dev);
  691. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  692. /* device not translated by any IOMMU in the system? */
  693. if (_bdf > amd_iommu_last_bdf)
  694. return 0;
  695. *bdf = amd_iommu_alias_table[_bdf];
  696. *iommu = amd_iommu_rlookup_table[*bdf];
  697. if (*iommu == NULL)
  698. return 0;
  699. *domain = domain_for_device(*bdf);
  700. if (*domain == NULL) {
  701. dma_dom = find_protection_domain(*bdf);
  702. if (!dma_dom)
  703. dma_dom = (*iommu)->default_dom;
  704. *domain = &dma_dom->domain;
  705. set_device_domain(*iommu, *domain, *bdf);
  706. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  707. "device ", (*domain)->id);
  708. print_devid(_bdf, 1);
  709. }
  710. return 1;
  711. }
  712. /*
  713. * This is the generic map function. It maps one 4kb page at paddr to
  714. * the given address in the DMA address space for the domain.
  715. */
  716. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  717. struct dma_ops_domain *dom,
  718. unsigned long address,
  719. phys_addr_t paddr,
  720. int direction)
  721. {
  722. u64 *pte, __pte;
  723. WARN_ON(address > dom->aperture_size);
  724. paddr &= PAGE_MASK;
  725. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  726. pte += IOMMU_PTE_L0_INDEX(address);
  727. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  728. if (direction == DMA_TO_DEVICE)
  729. __pte |= IOMMU_PTE_IR;
  730. else if (direction == DMA_FROM_DEVICE)
  731. __pte |= IOMMU_PTE_IW;
  732. else if (direction == DMA_BIDIRECTIONAL)
  733. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  734. WARN_ON(*pte);
  735. *pte = __pte;
  736. return (dma_addr_t)address;
  737. }
  738. /*
  739. * The generic unmapping function for on page in the DMA address space.
  740. */
  741. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  742. struct dma_ops_domain *dom,
  743. unsigned long address)
  744. {
  745. u64 *pte;
  746. if (address >= dom->aperture_size)
  747. return;
  748. WARN_ON(address & 0xfffULL || address > dom->aperture_size);
  749. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  750. pte += IOMMU_PTE_L0_INDEX(address);
  751. WARN_ON(!*pte);
  752. *pte = 0ULL;
  753. }
  754. /*
  755. * This function contains common code for mapping of a physically
  756. * contiguous memory region into DMA address space. It is uses by all
  757. * mapping functions provided by this IOMMU driver.
  758. * Must be called with the domain lock held.
  759. */
  760. static dma_addr_t __map_single(struct device *dev,
  761. struct amd_iommu *iommu,
  762. struct dma_ops_domain *dma_dom,
  763. phys_addr_t paddr,
  764. size_t size,
  765. int dir,
  766. bool align,
  767. u64 dma_mask)
  768. {
  769. dma_addr_t offset = paddr & ~PAGE_MASK;
  770. dma_addr_t address, start;
  771. unsigned int pages;
  772. unsigned long align_mask = 0;
  773. int i;
  774. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  775. paddr &= PAGE_MASK;
  776. if (align)
  777. align_mask = (1UL << get_order(size)) - 1;
  778. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  779. dma_mask);
  780. if (unlikely(address == bad_dma_address))
  781. goto out;
  782. start = address;
  783. for (i = 0; i < pages; ++i) {
  784. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  785. paddr += PAGE_SIZE;
  786. start += PAGE_SIZE;
  787. }
  788. address += offset;
  789. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  790. iommu_flush_tlb(iommu, dma_dom->domain.id);
  791. dma_dom->need_flush = false;
  792. } else if (unlikely(iommu_has_npcache(iommu)))
  793. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  794. out:
  795. return address;
  796. }
  797. /*
  798. * Does the reverse of the __map_single function. Must be called with
  799. * the domain lock held too
  800. */
  801. static void __unmap_single(struct amd_iommu *iommu,
  802. struct dma_ops_domain *dma_dom,
  803. dma_addr_t dma_addr,
  804. size_t size,
  805. int dir)
  806. {
  807. dma_addr_t i, start;
  808. unsigned int pages;
  809. if ((dma_addr == 0) || (dma_addr + size > dma_dom->aperture_size))
  810. return;
  811. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  812. dma_addr &= PAGE_MASK;
  813. start = dma_addr;
  814. for (i = 0; i < pages; ++i) {
  815. dma_ops_domain_unmap(iommu, dma_dom, start);
  816. start += PAGE_SIZE;
  817. }
  818. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  819. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  820. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  821. dma_dom->need_flush = false;
  822. }
  823. }
  824. /*
  825. * The exported map_single function for dma_ops.
  826. */
  827. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  828. size_t size, int dir)
  829. {
  830. unsigned long flags;
  831. struct amd_iommu *iommu;
  832. struct protection_domain *domain;
  833. u16 devid;
  834. dma_addr_t addr;
  835. u64 dma_mask;
  836. if (!check_device(dev))
  837. return bad_dma_address;
  838. dma_mask = *dev->dma_mask;
  839. get_device_resources(dev, &iommu, &domain, &devid);
  840. if (iommu == NULL || domain == NULL)
  841. /* device not handled by any AMD IOMMU */
  842. return (dma_addr_t)paddr;
  843. spin_lock_irqsave(&domain->lock, flags);
  844. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  845. dma_mask);
  846. if (addr == bad_dma_address)
  847. goto out;
  848. if (unlikely(iommu->need_sync))
  849. iommu_completion_wait(iommu);
  850. out:
  851. spin_unlock_irqrestore(&domain->lock, flags);
  852. return addr;
  853. }
  854. /*
  855. * The exported unmap_single function for dma_ops.
  856. */
  857. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  858. size_t size, int dir)
  859. {
  860. unsigned long flags;
  861. struct amd_iommu *iommu;
  862. struct protection_domain *domain;
  863. u16 devid;
  864. if (!check_device(dev) ||
  865. !get_device_resources(dev, &iommu, &domain, &devid))
  866. /* device not handled by any AMD IOMMU */
  867. return;
  868. spin_lock_irqsave(&domain->lock, flags);
  869. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  870. if (unlikely(iommu->need_sync))
  871. iommu_completion_wait(iommu);
  872. spin_unlock_irqrestore(&domain->lock, flags);
  873. }
  874. /*
  875. * This is a special map_sg function which is used if we should map a
  876. * device which is not handled by an AMD IOMMU in the system.
  877. */
  878. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  879. int nelems, int dir)
  880. {
  881. struct scatterlist *s;
  882. int i;
  883. for_each_sg(sglist, s, nelems, i) {
  884. s->dma_address = (dma_addr_t)sg_phys(s);
  885. s->dma_length = s->length;
  886. }
  887. return nelems;
  888. }
  889. /*
  890. * The exported map_sg function for dma_ops (handles scatter-gather
  891. * lists).
  892. */
  893. static int map_sg(struct device *dev, struct scatterlist *sglist,
  894. int nelems, int dir)
  895. {
  896. unsigned long flags;
  897. struct amd_iommu *iommu;
  898. struct protection_domain *domain;
  899. u16 devid;
  900. int i;
  901. struct scatterlist *s;
  902. phys_addr_t paddr;
  903. int mapped_elems = 0;
  904. u64 dma_mask;
  905. if (!check_device(dev))
  906. return 0;
  907. dma_mask = *dev->dma_mask;
  908. get_device_resources(dev, &iommu, &domain, &devid);
  909. if (!iommu || !domain)
  910. return map_sg_no_iommu(dev, sglist, nelems, dir);
  911. spin_lock_irqsave(&domain->lock, flags);
  912. for_each_sg(sglist, s, nelems, i) {
  913. paddr = sg_phys(s);
  914. s->dma_address = __map_single(dev, iommu, domain->priv,
  915. paddr, s->length, dir, false,
  916. dma_mask);
  917. if (s->dma_address) {
  918. s->dma_length = s->length;
  919. mapped_elems++;
  920. } else
  921. goto unmap;
  922. }
  923. if (unlikely(iommu->need_sync))
  924. iommu_completion_wait(iommu);
  925. out:
  926. spin_unlock_irqrestore(&domain->lock, flags);
  927. return mapped_elems;
  928. unmap:
  929. for_each_sg(sglist, s, mapped_elems, i) {
  930. if (s->dma_address)
  931. __unmap_single(iommu, domain->priv, s->dma_address,
  932. s->dma_length, dir);
  933. s->dma_address = s->dma_length = 0;
  934. }
  935. mapped_elems = 0;
  936. goto out;
  937. }
  938. /*
  939. * The exported map_sg function for dma_ops (handles scatter-gather
  940. * lists).
  941. */
  942. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  943. int nelems, int dir)
  944. {
  945. unsigned long flags;
  946. struct amd_iommu *iommu;
  947. struct protection_domain *domain;
  948. struct scatterlist *s;
  949. u16 devid;
  950. int i;
  951. if (!check_device(dev) ||
  952. !get_device_resources(dev, &iommu, &domain, &devid))
  953. return;
  954. spin_lock_irqsave(&domain->lock, flags);
  955. for_each_sg(sglist, s, nelems, i) {
  956. __unmap_single(iommu, domain->priv, s->dma_address,
  957. s->dma_length, dir);
  958. s->dma_address = s->dma_length = 0;
  959. }
  960. if (unlikely(iommu->need_sync))
  961. iommu_completion_wait(iommu);
  962. spin_unlock_irqrestore(&domain->lock, flags);
  963. }
  964. /*
  965. * The exported alloc_coherent function for dma_ops.
  966. */
  967. static void *alloc_coherent(struct device *dev, size_t size,
  968. dma_addr_t *dma_addr, gfp_t flag)
  969. {
  970. unsigned long flags;
  971. void *virt_addr;
  972. struct amd_iommu *iommu;
  973. struct protection_domain *domain;
  974. u16 devid;
  975. phys_addr_t paddr;
  976. u64 dma_mask = dev->coherent_dma_mask;
  977. if (!check_device(dev))
  978. return NULL;
  979. if (!get_device_resources(dev, &iommu, &domain, &devid))
  980. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  981. flag |= __GFP_ZERO;
  982. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  983. if (!virt_addr)
  984. return 0;
  985. paddr = virt_to_phys(virt_addr);
  986. if (!iommu || !domain) {
  987. *dma_addr = (dma_addr_t)paddr;
  988. return virt_addr;
  989. }
  990. if (!dma_mask)
  991. dma_mask = *dev->dma_mask;
  992. spin_lock_irqsave(&domain->lock, flags);
  993. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  994. size, DMA_BIDIRECTIONAL, true, dma_mask);
  995. if (*dma_addr == bad_dma_address) {
  996. free_pages((unsigned long)virt_addr, get_order(size));
  997. virt_addr = NULL;
  998. goto out;
  999. }
  1000. if (unlikely(iommu->need_sync))
  1001. iommu_completion_wait(iommu);
  1002. out:
  1003. spin_unlock_irqrestore(&domain->lock, flags);
  1004. return virt_addr;
  1005. }
  1006. /*
  1007. * The exported free_coherent function for dma_ops.
  1008. */
  1009. static void free_coherent(struct device *dev, size_t size,
  1010. void *virt_addr, dma_addr_t dma_addr)
  1011. {
  1012. unsigned long flags;
  1013. struct amd_iommu *iommu;
  1014. struct protection_domain *domain;
  1015. u16 devid;
  1016. if (!check_device(dev))
  1017. return;
  1018. get_device_resources(dev, &iommu, &domain, &devid);
  1019. if (!iommu || !domain)
  1020. goto free_mem;
  1021. spin_lock_irqsave(&domain->lock, flags);
  1022. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1023. if (unlikely(iommu->need_sync))
  1024. iommu_completion_wait(iommu);
  1025. spin_unlock_irqrestore(&domain->lock, flags);
  1026. free_mem:
  1027. free_pages((unsigned long)virt_addr, get_order(size));
  1028. }
  1029. /*
  1030. * This function is called by the DMA layer to find out if we can handle a
  1031. * particular device. It is part of the dma_ops.
  1032. */
  1033. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1034. {
  1035. u16 bdf;
  1036. struct pci_dev *pcidev;
  1037. /* No device or no PCI device */
  1038. if (!dev || dev->bus != &pci_bus_type)
  1039. return 0;
  1040. pcidev = to_pci_dev(dev);
  1041. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1042. /* Out of our scope? */
  1043. if (bdf > amd_iommu_last_bdf)
  1044. return 0;
  1045. return 1;
  1046. }
  1047. /*
  1048. * The function for pre-allocating protection domains.
  1049. *
  1050. * If the driver core informs the DMA layer if a driver grabs a device
  1051. * we don't need to preallocate the protection domains anymore.
  1052. * For now we have to.
  1053. */
  1054. void prealloc_protection_domains(void)
  1055. {
  1056. struct pci_dev *dev = NULL;
  1057. struct dma_ops_domain *dma_dom;
  1058. struct amd_iommu *iommu;
  1059. int order = amd_iommu_aperture_order;
  1060. u16 devid;
  1061. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1062. devid = (dev->bus->number << 8) | dev->devfn;
  1063. if (devid > amd_iommu_last_bdf)
  1064. continue;
  1065. devid = amd_iommu_alias_table[devid];
  1066. if (domain_for_device(devid))
  1067. continue;
  1068. iommu = amd_iommu_rlookup_table[devid];
  1069. if (!iommu)
  1070. continue;
  1071. dma_dom = dma_ops_domain_alloc(iommu, order);
  1072. if (!dma_dom)
  1073. continue;
  1074. init_unity_mappings_for_device(dma_dom, devid);
  1075. dma_dom->target_dev = devid;
  1076. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1077. }
  1078. }
  1079. static struct dma_mapping_ops amd_iommu_dma_ops = {
  1080. .alloc_coherent = alloc_coherent,
  1081. .free_coherent = free_coherent,
  1082. .map_single = map_single,
  1083. .unmap_single = unmap_single,
  1084. .map_sg = map_sg,
  1085. .unmap_sg = unmap_sg,
  1086. .dma_supported = amd_iommu_dma_supported,
  1087. };
  1088. /*
  1089. * The function which clues the AMD IOMMU driver into dma_ops.
  1090. */
  1091. int __init amd_iommu_init_dma_ops(void)
  1092. {
  1093. struct amd_iommu *iommu;
  1094. int order = amd_iommu_aperture_order;
  1095. int ret;
  1096. /*
  1097. * first allocate a default protection domain for every IOMMU we
  1098. * found in the system. Devices not assigned to any other
  1099. * protection domain will be assigned to the default one.
  1100. */
  1101. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1102. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1103. if (iommu->default_dom == NULL)
  1104. return -ENOMEM;
  1105. ret = iommu_init_unity_mappings(iommu);
  1106. if (ret)
  1107. goto free_domains;
  1108. }
  1109. /*
  1110. * If device isolation is enabled, pre-allocate the protection
  1111. * domains for each device.
  1112. */
  1113. if (amd_iommu_isolate)
  1114. prealloc_protection_domains();
  1115. iommu_detected = 1;
  1116. force_iommu = 1;
  1117. bad_dma_address = 0;
  1118. #ifdef CONFIG_GART_IOMMU
  1119. gart_iommu_aperture_disabled = 1;
  1120. gart_iommu_aperture = 0;
  1121. #endif
  1122. /* Make the driver finally visible to the drivers */
  1123. dma_ops = &amd_iommu_dma_ops;
  1124. return 0;
  1125. free_domains:
  1126. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1127. if (iommu->default_dom)
  1128. dma_ops_domain_free(iommu->default_dom);
  1129. }
  1130. return ret;
  1131. }