amd_iommu.c 34 KB

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