amd_iommu.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. #ifdef CONFIG_IOMMU_API
  479. static void domain_id_free(int id)
  480. {
  481. unsigned long flags;
  482. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  483. if (id > 0 && id < MAX_DOMAIN_ID)
  484. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  485. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  486. }
  487. #endif
  488. /*
  489. * Used to reserve address ranges in the aperture (e.g. for exclusion
  490. * ranges.
  491. */
  492. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  493. unsigned long start_page,
  494. unsigned int pages)
  495. {
  496. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  497. if (start_page + pages > last_page)
  498. pages = last_page - start_page;
  499. iommu_area_reserve(dom->bitmap, start_page, pages);
  500. }
  501. static void free_pagetable(struct protection_domain *domain)
  502. {
  503. int i, j;
  504. u64 *p1, *p2, *p3;
  505. p1 = domain->pt_root;
  506. if (!p1)
  507. return;
  508. for (i = 0; i < 512; ++i) {
  509. if (!IOMMU_PTE_PRESENT(p1[i]))
  510. continue;
  511. p2 = IOMMU_PTE_PAGE(p1[i]);
  512. for (j = 0; j < 512; ++j) {
  513. if (!IOMMU_PTE_PRESENT(p2[j]))
  514. continue;
  515. p3 = IOMMU_PTE_PAGE(p2[j]);
  516. free_page((unsigned long)p3);
  517. }
  518. free_page((unsigned long)p2);
  519. }
  520. free_page((unsigned long)p1);
  521. domain->pt_root = NULL;
  522. }
  523. /*
  524. * Free a domain, only used if something went wrong in the
  525. * allocation path and we need to free an already allocated page table
  526. */
  527. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  528. {
  529. if (!dom)
  530. return;
  531. free_pagetable(&dom->domain);
  532. kfree(dom->pte_pages);
  533. kfree(dom->bitmap);
  534. kfree(dom);
  535. }
  536. /*
  537. * Allocates a new protection domain usable for the dma_ops functions.
  538. * It also intializes the page table and the address allocator data
  539. * structures required for the dma_ops interface
  540. */
  541. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  542. unsigned order)
  543. {
  544. struct dma_ops_domain *dma_dom;
  545. unsigned i, num_pte_pages;
  546. u64 *l2_pde;
  547. u64 address;
  548. /*
  549. * Currently the DMA aperture must be between 32 MB and 1GB in size
  550. */
  551. if ((order < 25) || (order > 30))
  552. return NULL;
  553. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  554. if (!dma_dom)
  555. return NULL;
  556. spin_lock_init(&dma_dom->domain.lock);
  557. dma_dom->domain.id = domain_id_alloc();
  558. if (dma_dom->domain.id == 0)
  559. goto free_dma_dom;
  560. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  561. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  562. dma_dom->domain.priv = dma_dom;
  563. if (!dma_dom->domain.pt_root)
  564. goto free_dma_dom;
  565. dma_dom->aperture_size = (1ULL << order);
  566. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  567. GFP_KERNEL);
  568. if (!dma_dom->bitmap)
  569. goto free_dma_dom;
  570. /*
  571. * mark the first page as allocated so we never return 0 as
  572. * a valid dma-address. So we can use 0 as error value
  573. */
  574. dma_dom->bitmap[0] = 1;
  575. dma_dom->next_bit = 0;
  576. dma_dom->need_flush = false;
  577. dma_dom->target_dev = 0xffff;
  578. /* Intialize the exclusion range if necessary */
  579. if (iommu->exclusion_start &&
  580. iommu->exclusion_start < dma_dom->aperture_size) {
  581. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  582. int pages = iommu_num_pages(iommu->exclusion_start,
  583. iommu->exclusion_length,
  584. PAGE_SIZE);
  585. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  586. }
  587. /*
  588. * At the last step, build the page tables so we don't need to
  589. * allocate page table pages in the dma_ops mapping/unmapping
  590. * path.
  591. */
  592. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  593. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  594. GFP_KERNEL);
  595. if (!dma_dom->pte_pages)
  596. goto free_dma_dom;
  597. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  598. if (l2_pde == NULL)
  599. goto free_dma_dom;
  600. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  601. for (i = 0; i < num_pte_pages; ++i) {
  602. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  603. if (!dma_dom->pte_pages[i])
  604. goto free_dma_dom;
  605. address = virt_to_phys(dma_dom->pte_pages[i]);
  606. l2_pde[i] = IOMMU_L1_PDE(address);
  607. }
  608. return dma_dom;
  609. free_dma_dom:
  610. dma_ops_domain_free(dma_dom);
  611. return NULL;
  612. }
  613. /*
  614. * Find out the protection domain structure for a given PCI device. This
  615. * will give us the pointer to the page table root for example.
  616. */
  617. static struct protection_domain *domain_for_device(u16 devid)
  618. {
  619. struct protection_domain *dom;
  620. unsigned long flags;
  621. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  622. dom = amd_iommu_pd_table[devid];
  623. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  624. return dom;
  625. }
  626. /*
  627. * If a device is not yet associated with a domain, this function does
  628. * assigns it visible for the hardware
  629. */
  630. static void set_device_domain(struct amd_iommu *iommu,
  631. struct protection_domain *domain,
  632. u16 devid)
  633. {
  634. unsigned long flags;
  635. u64 pte_root = virt_to_phys(domain->pt_root);
  636. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  637. << DEV_ENTRY_MODE_SHIFT;
  638. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  639. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  640. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  641. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  642. amd_iommu_dev_table[devid].data[2] = domain->id;
  643. amd_iommu_pd_table[devid] = domain;
  644. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  645. iommu_queue_inv_dev_entry(iommu, devid);
  646. }
  647. /*****************************************************************************
  648. *
  649. * The next functions belong to the dma_ops mapping/unmapping code.
  650. *
  651. *****************************************************************************/
  652. /*
  653. * This function checks if the driver got a valid device from the caller to
  654. * avoid dereferencing invalid pointers.
  655. */
  656. static bool check_device(struct device *dev)
  657. {
  658. if (!dev || !dev->dma_mask)
  659. return false;
  660. return true;
  661. }
  662. /*
  663. * In this function the list of preallocated protection domains is traversed to
  664. * find the domain for a specific device
  665. */
  666. static struct dma_ops_domain *find_protection_domain(u16 devid)
  667. {
  668. struct dma_ops_domain *entry, *ret = NULL;
  669. unsigned long flags;
  670. if (list_empty(&iommu_pd_list))
  671. return NULL;
  672. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  673. list_for_each_entry(entry, &iommu_pd_list, list) {
  674. if (entry->target_dev == devid) {
  675. ret = entry;
  676. list_del(&ret->list);
  677. break;
  678. }
  679. }
  680. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  681. return ret;
  682. }
  683. /*
  684. * In the dma_ops path we only have the struct device. This function
  685. * finds the corresponding IOMMU, the protection domain and the
  686. * requestor id for a given device.
  687. * If the device is not yet associated with a domain this is also done
  688. * in this function.
  689. */
  690. static int get_device_resources(struct device *dev,
  691. struct amd_iommu **iommu,
  692. struct protection_domain **domain,
  693. u16 *bdf)
  694. {
  695. struct dma_ops_domain *dma_dom;
  696. struct pci_dev *pcidev;
  697. u16 _bdf;
  698. *iommu = NULL;
  699. *domain = NULL;
  700. *bdf = 0xffff;
  701. if (dev->bus != &pci_bus_type)
  702. return 0;
  703. pcidev = to_pci_dev(dev);
  704. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  705. /* device not translated by any IOMMU in the system? */
  706. if (_bdf > amd_iommu_last_bdf)
  707. return 0;
  708. *bdf = amd_iommu_alias_table[_bdf];
  709. *iommu = amd_iommu_rlookup_table[*bdf];
  710. if (*iommu == NULL)
  711. return 0;
  712. *domain = domain_for_device(*bdf);
  713. if (*domain == NULL) {
  714. dma_dom = find_protection_domain(*bdf);
  715. if (!dma_dom)
  716. dma_dom = (*iommu)->default_dom;
  717. *domain = &dma_dom->domain;
  718. set_device_domain(*iommu, *domain, *bdf);
  719. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  720. "device ", (*domain)->id);
  721. print_devid(_bdf, 1);
  722. }
  723. if (domain_for_device(_bdf) == NULL)
  724. set_device_domain(*iommu, *domain, _bdf);
  725. return 1;
  726. }
  727. /*
  728. * This is the generic map function. It maps one 4kb page at paddr to
  729. * the given address in the DMA address space for the domain.
  730. */
  731. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  732. struct dma_ops_domain *dom,
  733. unsigned long address,
  734. phys_addr_t paddr,
  735. int direction)
  736. {
  737. u64 *pte, __pte;
  738. WARN_ON(address > dom->aperture_size);
  739. paddr &= PAGE_MASK;
  740. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  741. pte += IOMMU_PTE_L0_INDEX(address);
  742. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  743. if (direction == DMA_TO_DEVICE)
  744. __pte |= IOMMU_PTE_IR;
  745. else if (direction == DMA_FROM_DEVICE)
  746. __pte |= IOMMU_PTE_IW;
  747. else if (direction == DMA_BIDIRECTIONAL)
  748. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  749. WARN_ON(*pte);
  750. *pte = __pte;
  751. return (dma_addr_t)address;
  752. }
  753. /*
  754. * The generic unmapping function for on page in the DMA address space.
  755. */
  756. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  757. struct dma_ops_domain *dom,
  758. unsigned long address)
  759. {
  760. u64 *pte;
  761. if (address >= dom->aperture_size)
  762. return;
  763. WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size);
  764. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  765. pte += IOMMU_PTE_L0_INDEX(address);
  766. WARN_ON(!*pte);
  767. *pte = 0ULL;
  768. }
  769. /*
  770. * This function contains common code for mapping of a physically
  771. * contiguous memory region into DMA address space. It is used by all
  772. * mapping functions provided with this IOMMU driver.
  773. * Must be called with the domain lock held.
  774. */
  775. static dma_addr_t __map_single(struct device *dev,
  776. struct amd_iommu *iommu,
  777. struct dma_ops_domain *dma_dom,
  778. phys_addr_t paddr,
  779. size_t size,
  780. int dir,
  781. bool align,
  782. u64 dma_mask)
  783. {
  784. dma_addr_t offset = paddr & ~PAGE_MASK;
  785. dma_addr_t address, start;
  786. unsigned int pages;
  787. unsigned long align_mask = 0;
  788. int i;
  789. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  790. paddr &= PAGE_MASK;
  791. if (align)
  792. align_mask = (1UL << get_order(size)) - 1;
  793. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  794. dma_mask);
  795. if (unlikely(address == bad_dma_address))
  796. goto out;
  797. start = address;
  798. for (i = 0; i < pages; ++i) {
  799. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  800. paddr += PAGE_SIZE;
  801. start += PAGE_SIZE;
  802. }
  803. address += offset;
  804. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  805. iommu_flush_tlb(iommu, dma_dom->domain.id);
  806. dma_dom->need_flush = false;
  807. } else if (unlikely(iommu_has_npcache(iommu)))
  808. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  809. out:
  810. return address;
  811. }
  812. /*
  813. * Does the reverse of the __map_single function. Must be called with
  814. * the domain lock held too
  815. */
  816. static void __unmap_single(struct amd_iommu *iommu,
  817. struct dma_ops_domain *dma_dom,
  818. dma_addr_t dma_addr,
  819. size_t size,
  820. int dir)
  821. {
  822. dma_addr_t i, start;
  823. unsigned int pages;
  824. if ((dma_addr == bad_dma_address) ||
  825. (dma_addr + size > dma_dom->aperture_size))
  826. return;
  827. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  828. dma_addr &= PAGE_MASK;
  829. start = dma_addr;
  830. for (i = 0; i < pages; ++i) {
  831. dma_ops_domain_unmap(iommu, dma_dom, start);
  832. start += PAGE_SIZE;
  833. }
  834. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  835. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  836. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  837. dma_dom->need_flush = false;
  838. }
  839. }
  840. /*
  841. * The exported map_single function for dma_ops.
  842. */
  843. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  844. size_t size, int dir)
  845. {
  846. unsigned long flags;
  847. struct amd_iommu *iommu;
  848. struct protection_domain *domain;
  849. u16 devid;
  850. dma_addr_t addr;
  851. u64 dma_mask;
  852. if (!check_device(dev))
  853. return bad_dma_address;
  854. dma_mask = *dev->dma_mask;
  855. get_device_resources(dev, &iommu, &domain, &devid);
  856. if (iommu == NULL || domain == NULL)
  857. /* device not handled by any AMD IOMMU */
  858. return (dma_addr_t)paddr;
  859. spin_lock_irqsave(&domain->lock, flags);
  860. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  861. dma_mask);
  862. if (addr == bad_dma_address)
  863. goto out;
  864. iommu_completion_wait(iommu);
  865. out:
  866. spin_unlock_irqrestore(&domain->lock, flags);
  867. return addr;
  868. }
  869. /*
  870. * The exported unmap_single function for dma_ops.
  871. */
  872. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  873. size_t size, int dir)
  874. {
  875. unsigned long flags;
  876. struct amd_iommu *iommu;
  877. struct protection_domain *domain;
  878. u16 devid;
  879. if (!check_device(dev) ||
  880. !get_device_resources(dev, &iommu, &domain, &devid))
  881. /* device not handled by any AMD IOMMU */
  882. return;
  883. spin_lock_irqsave(&domain->lock, flags);
  884. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  885. iommu_completion_wait(iommu);
  886. spin_unlock_irqrestore(&domain->lock, flags);
  887. }
  888. /*
  889. * This is a special map_sg function which is used if we should map a
  890. * device which is not handled by an AMD IOMMU in the system.
  891. */
  892. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  893. int nelems, int dir)
  894. {
  895. struct scatterlist *s;
  896. int i;
  897. for_each_sg(sglist, s, nelems, i) {
  898. s->dma_address = (dma_addr_t)sg_phys(s);
  899. s->dma_length = s->length;
  900. }
  901. return nelems;
  902. }
  903. /*
  904. * The exported map_sg function for dma_ops (handles scatter-gather
  905. * lists).
  906. */
  907. static int map_sg(struct device *dev, struct scatterlist *sglist,
  908. int nelems, int dir)
  909. {
  910. unsigned long flags;
  911. struct amd_iommu *iommu;
  912. struct protection_domain *domain;
  913. u16 devid;
  914. int i;
  915. struct scatterlist *s;
  916. phys_addr_t paddr;
  917. int mapped_elems = 0;
  918. u64 dma_mask;
  919. if (!check_device(dev))
  920. return 0;
  921. dma_mask = *dev->dma_mask;
  922. get_device_resources(dev, &iommu, &domain, &devid);
  923. if (!iommu || !domain)
  924. return map_sg_no_iommu(dev, sglist, nelems, dir);
  925. spin_lock_irqsave(&domain->lock, flags);
  926. for_each_sg(sglist, s, nelems, i) {
  927. paddr = sg_phys(s);
  928. s->dma_address = __map_single(dev, iommu, domain->priv,
  929. paddr, s->length, dir, false,
  930. dma_mask);
  931. if (s->dma_address) {
  932. s->dma_length = s->length;
  933. mapped_elems++;
  934. } else
  935. goto unmap;
  936. }
  937. iommu_completion_wait(iommu);
  938. out:
  939. spin_unlock_irqrestore(&domain->lock, flags);
  940. return mapped_elems;
  941. unmap:
  942. for_each_sg(sglist, s, mapped_elems, i) {
  943. if (s->dma_address)
  944. __unmap_single(iommu, domain->priv, s->dma_address,
  945. s->dma_length, dir);
  946. s->dma_address = s->dma_length = 0;
  947. }
  948. mapped_elems = 0;
  949. goto out;
  950. }
  951. /*
  952. * The exported map_sg function for dma_ops (handles scatter-gather
  953. * lists).
  954. */
  955. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  956. int nelems, int dir)
  957. {
  958. unsigned long flags;
  959. struct amd_iommu *iommu;
  960. struct protection_domain *domain;
  961. struct scatterlist *s;
  962. u16 devid;
  963. int i;
  964. if (!check_device(dev) ||
  965. !get_device_resources(dev, &iommu, &domain, &devid))
  966. return;
  967. spin_lock_irqsave(&domain->lock, flags);
  968. for_each_sg(sglist, s, nelems, i) {
  969. __unmap_single(iommu, domain->priv, s->dma_address,
  970. s->dma_length, dir);
  971. s->dma_address = s->dma_length = 0;
  972. }
  973. iommu_completion_wait(iommu);
  974. spin_unlock_irqrestore(&domain->lock, flags);
  975. }
  976. /*
  977. * The exported alloc_coherent function for dma_ops.
  978. */
  979. static void *alloc_coherent(struct device *dev, size_t size,
  980. dma_addr_t *dma_addr, gfp_t flag)
  981. {
  982. unsigned long flags;
  983. void *virt_addr;
  984. struct amd_iommu *iommu;
  985. struct protection_domain *domain;
  986. u16 devid;
  987. phys_addr_t paddr;
  988. u64 dma_mask = dev->coherent_dma_mask;
  989. if (!check_device(dev))
  990. return NULL;
  991. if (!get_device_resources(dev, &iommu, &domain, &devid))
  992. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  993. flag |= __GFP_ZERO;
  994. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  995. if (!virt_addr)
  996. return 0;
  997. paddr = virt_to_phys(virt_addr);
  998. if (!iommu || !domain) {
  999. *dma_addr = (dma_addr_t)paddr;
  1000. return virt_addr;
  1001. }
  1002. if (!dma_mask)
  1003. dma_mask = *dev->dma_mask;
  1004. spin_lock_irqsave(&domain->lock, flags);
  1005. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  1006. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1007. if (*dma_addr == bad_dma_address) {
  1008. free_pages((unsigned long)virt_addr, get_order(size));
  1009. virt_addr = NULL;
  1010. goto out;
  1011. }
  1012. iommu_completion_wait(iommu);
  1013. out:
  1014. spin_unlock_irqrestore(&domain->lock, flags);
  1015. return virt_addr;
  1016. }
  1017. /*
  1018. * The exported free_coherent function for dma_ops.
  1019. */
  1020. static void free_coherent(struct device *dev, size_t size,
  1021. void *virt_addr, dma_addr_t dma_addr)
  1022. {
  1023. unsigned long flags;
  1024. struct amd_iommu *iommu;
  1025. struct protection_domain *domain;
  1026. u16 devid;
  1027. if (!check_device(dev))
  1028. return;
  1029. get_device_resources(dev, &iommu, &domain, &devid);
  1030. if (!iommu || !domain)
  1031. goto free_mem;
  1032. spin_lock_irqsave(&domain->lock, flags);
  1033. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1034. iommu_completion_wait(iommu);
  1035. spin_unlock_irqrestore(&domain->lock, flags);
  1036. free_mem:
  1037. free_pages((unsigned long)virt_addr, get_order(size));
  1038. }
  1039. /*
  1040. * This function is called by the DMA layer to find out if we can handle a
  1041. * particular device. It is part of the dma_ops.
  1042. */
  1043. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1044. {
  1045. u16 bdf;
  1046. struct pci_dev *pcidev;
  1047. /* No device or no PCI device */
  1048. if (!dev || dev->bus != &pci_bus_type)
  1049. return 0;
  1050. pcidev = to_pci_dev(dev);
  1051. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1052. /* Out of our scope? */
  1053. if (bdf > amd_iommu_last_bdf)
  1054. return 0;
  1055. return 1;
  1056. }
  1057. /*
  1058. * The function for pre-allocating protection domains.
  1059. *
  1060. * If the driver core informs the DMA layer if a driver grabs a device
  1061. * we don't need to preallocate the protection domains anymore.
  1062. * For now we have to.
  1063. */
  1064. void prealloc_protection_domains(void)
  1065. {
  1066. struct pci_dev *dev = NULL;
  1067. struct dma_ops_domain *dma_dom;
  1068. struct amd_iommu *iommu;
  1069. int order = amd_iommu_aperture_order;
  1070. u16 devid;
  1071. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1072. devid = (dev->bus->number << 8) | dev->devfn;
  1073. if (devid > amd_iommu_last_bdf)
  1074. continue;
  1075. devid = amd_iommu_alias_table[devid];
  1076. if (domain_for_device(devid))
  1077. continue;
  1078. iommu = amd_iommu_rlookup_table[devid];
  1079. if (!iommu)
  1080. continue;
  1081. dma_dom = dma_ops_domain_alloc(iommu, order);
  1082. if (!dma_dom)
  1083. continue;
  1084. init_unity_mappings_for_device(dma_dom, devid);
  1085. dma_dom->target_dev = devid;
  1086. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1087. }
  1088. }
  1089. static struct dma_mapping_ops amd_iommu_dma_ops = {
  1090. .alloc_coherent = alloc_coherent,
  1091. .free_coherent = free_coherent,
  1092. .map_single = map_single,
  1093. .unmap_single = unmap_single,
  1094. .map_sg = map_sg,
  1095. .unmap_sg = unmap_sg,
  1096. .dma_supported = amd_iommu_dma_supported,
  1097. };
  1098. /*
  1099. * The function which clues the AMD IOMMU driver into dma_ops.
  1100. */
  1101. int __init amd_iommu_init_dma_ops(void)
  1102. {
  1103. struct amd_iommu *iommu;
  1104. int order = amd_iommu_aperture_order;
  1105. int ret;
  1106. /*
  1107. * first allocate a default protection domain for every IOMMU we
  1108. * found in the system. Devices not assigned to any other
  1109. * protection domain will be assigned to the default one.
  1110. */
  1111. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1112. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1113. if (iommu->default_dom == NULL)
  1114. return -ENOMEM;
  1115. ret = iommu_init_unity_mappings(iommu);
  1116. if (ret)
  1117. goto free_domains;
  1118. }
  1119. /*
  1120. * If device isolation is enabled, pre-allocate the protection
  1121. * domains for each device.
  1122. */
  1123. if (amd_iommu_isolate)
  1124. prealloc_protection_domains();
  1125. iommu_detected = 1;
  1126. force_iommu = 1;
  1127. bad_dma_address = 0;
  1128. #ifdef CONFIG_GART_IOMMU
  1129. gart_iommu_aperture_disabled = 1;
  1130. gart_iommu_aperture = 0;
  1131. #endif
  1132. /* Make the driver finally visible to the drivers */
  1133. dma_ops = &amd_iommu_dma_ops;
  1134. return 0;
  1135. free_domains:
  1136. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1137. if (iommu->default_dom)
  1138. dma_ops_domain_free(iommu->default_dom);
  1139. }
  1140. return ret;
  1141. }