21 #include <linux/pci.h>
23 #include <linux/bitmap.h>
24 #include <linux/slab.h>
33 #include <linux/export.h>
36 #include <asm/irq_remapping.h>
39 #include <asm/hw_irq.h>
40 #include <asm/msidef.h>
42 #include <asm/iommu.h>
50 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
52 #define LOOP_TIMEOUT 100000
70 #define AMD_IOMMU_PGSIZES (~0xFFFUL)
108 static int __init alloc_passthrough_domain(
void);
121 dev_data = kzalloc(
sizeof(*dev_data),
GFP_KERNEL);
130 spin_unlock_irqrestore(&dev_data_list_lock, flags);
141 spin_unlock_irqrestore(&dev_data_list_lock, flags);
153 if (dev_data->
devid == devid)
160 spin_unlock_irqrestore(&dev_data_list_lock, flags);
169 dev_data = search_dev_data(devid);
171 if (dev_data ==
NULL)
172 dev_data = alloc_dev_data(devid);
181 return calc_devid(pdev->
bus->number, pdev->
devfn);
189 static bool pci_iommuv2_capable(
struct pci_dev *pdev)
191 static const int caps[] = {
198 for (i = 0; i < 3; ++
i) {
207 static bool pdev_pri_erratum(
struct pci_dev *pdev,
u32 erratum)
211 dev_data = get_dev_data(&pdev->
dev);
213 return dev_data->
errata & (1 << erratum) ?
true :
false;
226 if (list_empty(&iommu_pd_list))
239 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
248 static bool check_device(
struct device *
dev)
259 devid = get_device_id(dev);
277 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
279 static int iommu_init_device(
struct device *dev)
290 dev_data = find_dev_data(get_device_id(dev));
295 if (alias != dev_data->
devid) {
298 alias_data = find_dev_data(alias);
299 if (alias_data ==
NULL) {
300 pr_err(
"AMD-Vi: Warning: Unhandled device %s\n",
302 free_dev_data(dev_data);
310 if (dma_pdev ==
NULL)
322 swap_pci_ref(&dma_pdev,
332 while (!pci_is_root_bus(dma_pdev->
bus)) {
336 if (!pci_is_root_bus(bus))
354 return PTR_ERR(group);
364 if (pci_iommuv2_capable(pdev)) {
376 static void iommu_ignore_device(
struct device *dev)
380 devid = get_device_id(dev);
390 static void iommu_uninit_device(
struct device *dev)
408 if (!check_device(&pdev->
dev))
411 iommu_uninit_device(&pdev->
dev);
416 free_dev_data(dev_data);
426 if (!check_device(&pdev->
dev))
429 ret = iommu_init_device(&pdev->
dev);
431 iommu_ignore_device(&pdev->
dev);
444 #ifdef CONFIG_AMD_IOMMU_STATS
467 static struct dentry *stats_dir;
468 static struct dentry *de_fflush;
470 static void amd_iommu_stats_add(
struct __iommu_counter *
cnt)
472 if (stats_dir ==
NULL)
479 static void amd_iommu_stats_init(
void)
482 if (stats_dir ==
NULL)
488 amd_iommu_stats_add(&compl_wait);
489 amd_iommu_stats_add(&cnt_map_single);
490 amd_iommu_stats_add(&cnt_unmap_single);
491 amd_iommu_stats_add(&cnt_map_sg);
492 amd_iommu_stats_add(&cnt_unmap_sg);
493 amd_iommu_stats_add(&cnt_alloc_coherent);
494 amd_iommu_stats_add(&cnt_free_coherent);
495 amd_iommu_stats_add(&cross_page);
496 amd_iommu_stats_add(&domain_flush_single);
497 amd_iommu_stats_add(&domain_flush_all);
498 amd_iommu_stats_add(&alloced_io_mem);
499 amd_iommu_stats_add(&total_map_requests);
500 amd_iommu_stats_add(&complete_ppr);
501 amd_iommu_stats_add(&invalidate_iotlb);
502 amd_iommu_stats_add(&invalidate_iotlb_all);
503 amd_iommu_stats_add(&pri_requests);
514 static void dump_dte_entry(
u16 devid)
518 for (i = 0; i < 4; ++
i)
519 pr_err(
"AMD-Vi: DTE[%d]: %016llx\n", i,
523 static void dump_command(
unsigned long phys_addr)
528 for (i = 0; i < 4; ++
i)
529 pr_err(
"AMD-Vi: CMD[%d]: %08x\n", i, cmd->
data[i]);
532 static void iommu_print_event(
struct amd_iommu *
iommu,
void *__evt)
535 volatile u32 *
event = __evt;
544 address = (
u64)(((
u64)
event[3]) << 32) |
event[2];
549 pr_err(
"AMD-Vi: No event written to event log\n");
560 printk(
"ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
561 "address=0x%016llx flags=0x%04x]\n",
564 dump_dte_entry(devid);
567 printk(
"IO_PAGE_FAULT device=%02x:%02x.%x "
568 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
570 domid, address, flags);
573 printk(
"DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
574 "address=0x%016llx flags=0x%04x]\n",
579 printk(
"PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
580 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
582 domid, address, flags);
585 printk(
"ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
586 dump_command(address);
589 printk(
"COMMAND_HARDWARE_ERROR address=0x%016llx "
590 "flags=0x%04x]\n", address, flags);
593 printk(
"IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
594 "address=0x%016llx]\n",
599 printk(
"INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
600 "address=0x%016llx flags=0x%04x]\n",
611 static void iommu_poll_events(
struct amd_iommu *iommu)
621 while (head != tail) {
622 iommu_print_event(iommu, iommu->
evt_buf + head);
628 spin_unlock_irqrestore(&iommu->
lock, flags);
651 static void iommu_poll_ppr_log(
struct amd_iommu *iommu)
667 while (head != tail) {
693 raw[0] = raw[1] = 0
UL;
703 spin_unlock_irqrestore(&iommu->
lock, flags);
706 iommu_handle_ppr_entry(iommu, entry);
715 spin_unlock_irqrestore(&iommu->
lock, flags);
723 iommu_poll_events(iommu);
724 iommu_poll_ppr_log(iommu);
741 static int wait_on_sem(
volatile u64 *
sem)
745 while (*sem == 0 && i < LOOP_TIMEOUT) {
750 if (i == LOOP_TIMEOUT) {
751 pr_alert(
"AMD-Vi: Completion-Wait loop timed out\n");
758 static void copy_cmd_to_buffer(
struct amd_iommu *iommu,
768 memcpy(target, cmd,
sizeof(*cmd));
774 static void build_completion_wait(
struct iommu_cmd *cmd,
u64 address)
778 memset(cmd, 0,
sizeof(*cmd));
785 static void build_inv_dte(
struct iommu_cmd *cmd,
u16 devid)
787 memset(cmd, 0,
sizeof(*cmd));
792 static void build_inv_iommu_pages(
struct iommu_cmd *cmd,
u64 address,
793 size_t size,
u16 domid,
int pde)
798 pages = iommu_num_pages(address, size,
PAGE_SIZE);
812 memset(cmd, 0,
sizeof(*cmd));
823 static void build_inv_iotlb_pages(
struct iommu_cmd *cmd,
u16 devid,
int qdep,
824 u64 address,
size_t size)
829 pages = iommu_num_pages(address, size,
PAGE_SIZE);
843 memset(cmd, 0,
sizeof(*cmd));
845 cmd->
data[0] |= (qdep & 0xff) << 24;
854 static void build_inv_iommu_pasid(
struct iommu_cmd *cmd,
u16 domid,
int pasid,
855 u64 address,
bool size)
857 memset(cmd, 0,
sizeof(*cmd));
859 address &= ~(0xfffULL);
872 static void build_inv_iotlb_pasid(
struct iommu_cmd *cmd,
u16 devid,
int pasid,
873 int qdep,
u64 address,
bool size)
875 memset(cmd, 0,
sizeof(*cmd));
877 address &= ~(0xfffULL);
880 cmd->
data[0] |= (pasid & 0xff) << 16;
881 cmd->
data[0] |= (qdep & 0xff) << 24;
883 cmd->
data[1] |= ((pasid >> 8) & 0xfff) << 16;
892 static void build_complete_ppr(
struct iommu_cmd *cmd,
u16 devid,
int pasid,
895 memset(cmd, 0,
sizeof(*cmd));
902 cmd->
data[3] = tag & 0x1ff;
908 static void build_inv_all(
struct iommu_cmd *cmd)
910 memset(cmd, 0,
sizeof(*cmd));
914 static void build_inv_irt(
struct iommu_cmd *cmd,
u16 devid)
916 memset(cmd, 0,
sizeof(*cmd));
925 static int iommu_queue_command_sync(
struct amd_iommu *iommu,
939 next_tail = (tail +
sizeof(*cmd)) % iommu->
cmd_buf_size;
944 volatile u64 sem = 0;
947 build_completion_wait(&sync_cmd, (
u64)&sem);
948 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
950 spin_unlock_irqrestore(&iommu->
lock, flags);
952 if ((ret = wait_on_sem(&sem)) != 0)
958 copy_cmd_to_buffer(iommu, cmd, tail);
963 spin_unlock_irqrestore(&iommu->
lock, flags);
970 return iommu_queue_command_sync(iommu, cmd,
true);
977 static int iommu_completion_wait(
struct amd_iommu *iommu)
980 volatile u64 sem = 0;
986 build_completion_wait(&cmd, (
u64)&sem);
988 ret = iommu_queue_command_sync(iommu, &cmd,
false);
992 return wait_on_sem(&sem);
995 static int iommu_flush_dte(
struct amd_iommu *iommu,
u16 devid)
999 build_inv_dte(&cmd, devid);
1001 return iommu_queue_command(iommu, &cmd);
1004 static void iommu_flush_dte_all(
struct amd_iommu *iommu)
1008 for (devid = 0; devid <= 0xffff; ++
devid)
1009 iommu_flush_dte(iommu, devid);
1011 iommu_completion_wait(iommu);
1018 static void iommu_flush_tlb_all(
struct amd_iommu *iommu)
1022 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1026 iommu_queue_command(iommu, &cmd);
1029 iommu_completion_wait(iommu);
1032 static void iommu_flush_all(
struct amd_iommu *iommu)
1036 build_inv_all(&cmd);
1038 iommu_queue_command(iommu, &cmd);
1039 iommu_completion_wait(iommu);
1042 static void iommu_flush_irt(
struct amd_iommu *iommu,
u16 devid)
1046 build_inv_irt(&cmd, devid);
1048 iommu_queue_command(iommu, &cmd);
1051 static void iommu_flush_irt_all(
struct amd_iommu *iommu)
1056 iommu_flush_irt(iommu, devid);
1058 iommu_completion_wait(iommu);
1064 iommu_flush_all(iommu);
1066 iommu_flush_dte_all(iommu);
1067 iommu_flush_irt_all(iommu);
1068 iommu_flush_tlb_all(iommu);
1076 u64 address,
size_t size)
1082 qdep = dev_data->
ats.qdep;
1085 build_inv_iotlb_pages(&cmd, dev_data->
devid, qdep, address, size);
1087 return iommu_queue_command(iommu, &cmd);
1100 ret = iommu_flush_dte(iommu, dev_data->
devid);
1104 if (dev_data->
ats.enabled)
1105 ret = device_flush_iotlb(dev_data, 0, ~0
UL);
1116 u64 address,
size_t size,
int pde)
1122 build_inv_iommu_pages(&cmd, address, size, domain->
id, pde);
1132 ret |= iommu_queue_command(
amd_iommus[i], &cmd);
1137 if (!dev_data->
ats.enabled)
1140 ret |= device_flush_iotlb(dev_data, address, size);
1147 u64 address,
size_t size)
1149 __domain_flush_pages(domain, address, size, 0);
1189 device_flush_dte(dev_data);
1219 domain->pt_root =
pte;
1221 domain->updated =
true;
1227 unsigned long address,
1238 increase_address_space(domain, gfp);
1240 level = domain->
mode - 1;
1245 while (level > end_lvl) {
1261 if (pte_page && level == end_lvl)
1282 level = domain->
mode - 1;
1293 unsigned long pte_mask,
__pte;
1301 __pte = ((
unsigned long)pte) & pte_mask;
1328 unsigned long bus_addr,
1329 unsigned long phys_addr,
1331 unsigned long page_size)
1344 for (i = 0; i <
count; ++
i)
1359 for (i = 0; i <
count; ++
i)
1368 unsigned long bus_addr,
1369 unsigned long page_size)
1371 unsigned long long unmap_size, unmapped;
1378 while (unmapped < page_size) {
1380 pte = fetch_pte(dom, bus_addr);
1398 for (i = 0; i <
count; i++)
1402 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1403 unmapped += unmap_size;
1415 static int iommu_for_unity_map(
struct amd_iommu *iommu,
1420 for (i = entry->
devid_start; i <= entry->devid_end; ++i) {
1449 if (addr < dma_dom->aperture_size)
1463 static int iommu_init_unity_mappings(
struct amd_iommu *iommu)
1469 if (!iommu_for_unity_map(iommu, entry))
1471 ret = dma_ops_unity_map(iommu->
default_dom, entry);
1482 static int init_unity_mappings_for_device(
struct dma_ops_domain *dma_dom,
1491 ret = dma_ops_unity_map(dma_dom, e);
1519 static void dma_ops_reserve_addresses(
struct dma_ops_domain *dom,
1520 unsigned long start_page,
1525 if (start_page + pages > last_page)
1526 pages = last_page - start_page;
1528 for (i = start_page; i < start_page +
pages; ++
i) {
1541 bool populate,
gfp_t gfp)
1547 #ifdef CONFIG_IOMMU_STRESS
1559 if (!dma_dom->
aperture[index]->bitmap)
1569 for (i = 0; i < num_ptes; ++
i) {
1587 unsigned long spage;
1593 dma_ops_reserve_addresses(dma_dom, spage, pages);
1601 unsigned long startpage;
1606 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1616 for (i = dma_dom->
aperture[index]->offset;
1617 i < dma_dom->aperture_size;
1619 u64 *pte = fetch_pte(&dma_dom->
domain, i);
1623 dma_ops_reserve_addresses(dma_dom, i >>
PAGE_SHIFT, 1);
1626 update_domain(&dma_dom->
domain);
1631 update_domain(&dma_dom->
domain);
1641 static unsigned long dma_ops_area_alloc(
struct device *dev,
1644 unsigned long align_mask,
1646 unsigned long start)
1651 unsigned long boundary_size;
1652 unsigned long address = -1;
1653 unsigned long limit;
1657 boundary_size =
ALIGN(dma_get_seg_boundary(dev) + 1,
1660 for (;i < max_index; ++
i) {
1663 if (dom->
aperture[i]->offset >= dma_mask)
1670 limit, next_bit, pages, 0,
1671 boundary_size, align_mask);
1672 if (address != -1) {
1685 static unsigned long dma_ops_alloc_addresses(
struct device *dev,
1688 unsigned long align_mask,
1693 #ifdef CONFIG_IOMMU_STRESS
1698 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1701 if (address == -1) {
1703 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1722 unsigned long address,
1730 #ifdef CONFIG_IOMMU_STRESS
1759 unsigned long flags;
1762 list_add(&domain->
list, &amd_iommu_pd_list);
1772 unsigned long flags;
1779 static u16 domain_id_alloc(
void)
1781 unsigned long flags;
1796 static void domain_id_free(
int id)
1798 unsigned long flags;
1816 for (i = 0; i < 512; ++
i) {
1821 for (j = 0; j < 512; ++
j) {
1836 static void free_gcr3_tbl_level1(
u64 *tbl)
1841 for (i = 0; i < 512; ++
i) {
1851 static void free_gcr3_tbl_level2(
u64 *tbl)
1856 for (i = 0; i < 512; ++
i) {
1862 free_gcr3_tbl_level1(ptr);
1868 if (domain->
glx == 2)
1869 free_gcr3_tbl_level2(domain->
gcr3_tbl);
1870 else if (domain->
glx == 1)
1871 free_gcr3_tbl_level1(domain->
gcr3_tbl);
1872 else if (domain->
glx != 0)
1889 del_domain_from_list(&dom->
domain);
1891 free_pagetable(&dom->
domain);
1918 dma_dom->
domain.id = domain_id_alloc();
1919 if (dma_dom->
domain.id == 0)
1921 INIT_LIST_HEAD(&dma_dom->
domain.dev_list);
1925 dma_dom->
domain.priv = dma_dom;
1926 if (!dma_dom->
domain.pt_root)
1932 add_domain_to_list(&dma_dom->
domain);
1934 if (alloc_new_range(dma_dom,
true,
GFP_KERNEL))
1941 dma_dom->
aperture[0]->bitmap[0] = 1;
1948 dma_ops_domain_free(dma_dom);
2005 flags &= ~(0xffff
UL);
2006 flags |= domain->
id;
2012 static void clear_dte_entry(
u16 devid)
2028 ats = dev_data->
ats.enabled;
2031 dev_data->
domain = domain;
2033 set_dte_entry(dev_data->
devid, domain, ats);
2040 device_flush_dte(dev_data);
2051 dev_data->
domain->dev_cnt -= 1;
2056 clear_dte_entry(dev_data->
devid);
2059 device_flush_dte(dev_data);
2072 spin_lock(&domain->
lock);
2080 alias_data->
domain != domain)
2084 dev_data->
domain != domain)
2089 do_attach(alias_data, domain);
2095 do_attach(dev_data, domain);
2104 spin_unlock(&domain->
lock);
2110 static void pdev_iommuv2_disable(
struct pci_dev *pdev)
2113 pci_disable_pri(pdev);
2114 pci_disable_pasid(pdev);
2118 static int pri_reset_while_enabled(
struct pci_dev *pdev)
2127 pci_read_config_word(pdev, pos +
PCI_PRI_CTRL, &control);
2129 pci_write_config_word(pdev, pos +
PCI_PRI_CTRL, control);
2134 static int pdev_iommuv2_enable(
struct pci_dev *pdev)
2141 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2143 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
2146 ret = pci_enable_pasid(pdev, 0);
2151 ret = pci_reset_pri(pdev);
2156 ret = pci_enable_pri(pdev, reqs);
2161 ret = pri_reset_while_enabled(pdev);
2173 pci_disable_pri(pdev);
2174 pci_disable_pasid(pdev);
2180 #define PCI_PRI_TLP_OFF (1 << 15)
2182 static bool pci_pri_tlp_required(
struct pci_dev *pdev)
2200 static int attach_device(
struct device *dev,
2205 unsigned long flags;
2208 dev_data = get_dev_data(dev);
2214 if (pdev_iommuv2_enable(pdev) != 0)
2217 dev_data->
ats.enabled =
true;
2219 dev_data->
pri_tlp = pci_pri_tlp_required(pdev);
2220 }
else if (amd_iommu_iotlb_sup &&
2222 dev_data->
ats.enabled =
true;
2227 ret = __attach_device(dev_data, domain);
2235 domain_flush_tlb_pde(domain);
2246 unsigned long flags;
2250 domain = dev_data->
domain;
2258 do_detach(alias_data);
2262 do_detach(dev_data);
2264 spin_unlock_irqrestore(&domain->
lock, flags);
2272 (dev_data->
domain ==
NULL && domain != pt_domain))
2273 __attach_device(dev_data, pt_domain);
2279 static void detach_device(
struct device *dev)
2283 unsigned long flags;
2285 dev_data = get_dev_data(dev);
2286 domain = dev_data->
domain;
2290 __detach_device(dev_data);
2295 else if (dev_data->
ats.enabled)
2298 dev_data->
ats.enabled =
false;
2309 unsigned long flags;
2311 dev_data = get_dev_data(dev);
2321 __attach_device(dev_data, alias_data->
domain);
2322 dom = alias_data->
domain;
2338 unsigned long flags;
2341 if (!check_device(dev))
2344 devid = get_device_id(dev);
2346 dev_data = get_dev_data(dev);
2351 domain = domain_for_device(dev);
2361 iommu_init_device(dev);
2367 dev_data = get_dev_data(dev);
2369 if (iommu_pass_through || dev_data->
iommu_v2) {
2371 attach_device(dev, pt_domain);
2375 domain = domain_for_device(dev);
2378 dma_domain = find_protection_domain(devid);
2381 dma_domain = dma_ops_domain_alloc();
2388 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2390 dev_data = get_dev_data(dev);
2392 dev->
archdata.dma_ops = &amd_iommu_dma_ops;
2397 iommu_uninit_device(dev);
2403 iommu_completion_wait(iommu);
2410 .notifier_call = device_change_notifier,
2435 u16 devid = get_device_id(dev);
2437 if (!check_device(dev))
2440 domain = domain_for_device(dev);
2442 return ERR_PTR(-
EBUSY);
2448 dma_dom = find_protection_domain(devid);
2451 attach_device(dev, &dma_dom->
domain);
2452 DUMP_printk(
"Using protection domain %d for device %s\n",
2453 dma_dom->
domain.id, dev_name(dev));
2463 set_dte_entry(dev_data->devid, domain, dev_data->ats.
enabled);
2468 if (!domain->updated)
2471 update_device_table(domain);
2473 domain_flush_devices(domain);
2474 domain_flush_tlb_pde(domain);
2476 domain->updated =
false;
2483 unsigned long address)
2500 update_domain(&dom->
domain);
2510 unsigned long address,
2520 pte = dma_ops_get_pte(dom, address);
2544 unsigned long address)
2584 unsigned long align_mask = 0;
2587 pages = iommu_num_pages(paddr, size,
PAGE_SIZE);
2599 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2609 if (alloc_new_range(dma_dom,
false,
GFP_ATOMIC))
2620 for (i = 0; i <
pages; ++
i) {
2621 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
2633 domain_flush_tlb(&dma_dom->
domain);
2635 }
else if (
unlikely(amd_iommu_np_cache))
2636 domain_flush_pages(&dma_dom->
domain, address, size);
2643 for (--i; i >= 0; --
i) {
2645 dma_ops_domain_unmap(dma_dom, start);
2648 dma_ops_free_addresses(dma_dom, address, pages);
2671 pages = iommu_num_pages(dma_addr, size,
PAGE_SIZE);
2675 for (i = 0; i <
pages; ++
i) {
2676 dma_ops_domain_unmap(dma_dom, start);
2682 dma_ops_free_addresses(dma_dom, dma_addr, pages);
2685 domain_flush_pages(&dma_dom->
domain, flush_addr, size);
2694 unsigned long offset,
size_t size,
2698 unsigned long flags;
2706 domain = get_domain(dev);
2707 if (PTR_ERR(domain) == -
EINVAL)
2709 else if (IS_ERR(domain))
2716 addr = __map_single(dev, domain->
priv, paddr, size, dir,
false,
2721 domain_flush_complete(domain);
2724 spin_unlock_irqrestore(&domain->
lock, flags);
2732 static void unmap_page(
struct device *dev,
dma_addr_t dma_addr,
size_t size,
2735 unsigned long flags;
2740 domain = get_domain(dev);
2746 __unmap_single(domain->
priv, dma_addr, size, dir);
2748 domain_flush_complete(domain);
2750 spin_unlock_irqrestore(&domain->
lock, flags);
2758 int nelems,
int dir)
2765 s->dma_length = s->
length;
2779 unsigned long flags;
2784 int mapped_elems = 0;
2789 domain = get_domain(dev);
2790 if (PTR_ERR(domain) == -
EINVAL)
2791 return map_sg_no_iommu(dev, sglist, nelems, dir);
2792 else if (IS_ERR(domain))
2803 paddr, s->
length, dir,
false,
2807 s->dma_length = s->
length;
2813 domain_flush_complete(domain);
2816 spin_unlock_irqrestore(&domain->
lock, flags);
2818 return mapped_elems;
2823 s->dma_length, dir);
2840 unsigned long flags;
2847 domain = get_domain(dev);
2855 s->dma_length, dir);
2859 domain_flush_complete(domain);
2861 spin_unlock_irqrestore(&domain->
lock, flags);
2867 static void *alloc_coherent(
struct device *dev,
size_t size,
2871 unsigned long flags;
2879 domain = get_domain(dev);
2880 if (PTR_ERR(domain) == -
EINVAL) {
2882 *dma_addr =
__pa(virt_addr);
2884 }
else if (IS_ERR(domain))
2902 *dma_addr = __map_single(dev, domain->
priv, paddr,
2906 spin_unlock_irqrestore(&domain->
lock, flags);
2910 domain_flush_complete(domain);
2912 spin_unlock_irqrestore(&domain->
lock, flags);
2926 static void free_coherent(
struct device *dev,
size_t size,
2930 unsigned long flags;
2935 domain = get_domain(dev);
2943 domain_flush_complete(domain);
2945 spin_unlock_irqrestore(&domain->
lock, flags);
2955 static int amd_iommu_dma_supported(
struct device *dev,
u64 mask)
2957 return check_device(dev);
2967 static void __init prealloc_protection_domains(
void)
2977 if (!check_device(&dev->
dev))
2980 dev_data = get_dev_data(&dev->
dev);
2981 if (!amd_iommu_force_isolation && dev_data->
iommu_v2) {
2983 alloc_passthrough_domain();
2985 attach_device(&dev->
dev, pt_domain);
2986 pr_info(
"AMD-Vi: Using passthrough domain for device %s\n",
2987 dev_name(&dev->
dev));
2991 if (domain_for_device(&dev->
dev))
2994 devid = get_device_id(&dev->
dev);
2996 dma_dom = dma_ops_domain_alloc();
2999 init_unity_mappings_for_device(dma_dom, devid);
3002 attach_device(&dev->
dev, &dma_dom->
domain);
3009 .alloc = alloc_coherent,
3010 .free = free_coherent,
3015 .dma_supported = amd_iommu_dma_supported,
3018 static unsigned device_dma_ops_init(
void)
3025 if (!check_device(&pdev->
dev)) {
3027 iommu_ignore_device(&pdev->
dev);
3033 dev_data = get_dev_data(&pdev->
dev);
3036 pdev->
dev.archdata.dma_ops = &amd_iommu_dma_ops;
3068 ret = iommu_init_unity_mappings(iommu);
3076 prealloc_protection_domains();
3082 unhandled = device_dma_ops_init();
3088 amd_iommu_stats_init();
3091 pr_info(
"AMD-Vi: IO/TLB flush on unmap enabled\n");
3093 pr_info(
"AMD-Vi: Lazy IO/TLB flushing enabled\n");
3120 unsigned long flags;
3125 __detach_device(dev_data);
3137 del_domain_from_list(domain);
3140 domain_id_free(domain->
id);
3149 domain = kzalloc(
sizeof(*domain),
GFP_KERNEL);
3155 domain->
id = domain_id_alloc();
3160 add_domain_to_list(domain);
3170 static int __init alloc_passthrough_domain(
void)
3172 if (pt_domain !=
NULL)
3176 pt_domain = protection_domain_alloc();
3184 static int amd_iommu_domain_init(
struct iommu_domain *dom)
3188 domain = protection_domain_alloc();
3202 dom->
geometry.aperture_end = ~0ULL;
3203 dom->
geometry.force_aperture =
true;
3208 protection_domain_free(domain);
3213 static void amd_iommu_domain_destroy(
struct iommu_domain *dom)
3221 cleanup_domain(domain);
3226 free_pagetable(domain);
3229 free_gcr3_table(domain);
3231 protection_domain_free(domain);
3236 static void amd_iommu_detach_device(
struct iommu_domain *dom,
3243 if (!check_device(dev))
3246 devid = get_device_id(dev);
3255 iommu_completion_wait(iommu);
3258 static int amd_iommu_attach_device(
struct iommu_domain *dom,
3266 if (!check_device(dev))
3278 ret = attach_device(dev, domain);
3280 iommu_completion_wait(iommu);
3286 phys_addr_t paddr,
size_t page_size,
int iommu_prot)
3307 static size_t amd_iommu_unmap(
struct iommu_domain *dom,
unsigned long iova,
3320 domain_flush_tlb_pde(domain);
3329 unsigned long offset_mask;
3336 pte = fetch_pte(domain, iova);
3347 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
3352 static int amd_iommu_domain_has_cap(
struct iommu_domain *domain,
3365 static struct iommu_ops amd_iommu_ops = {
3366 .domain_init = amd_iommu_domain_init,
3367 .domain_destroy = amd_iommu_domain_destroy,
3368 .attach_dev = amd_iommu_attach_device,
3369 .detach_dev = amd_iommu_detach_device,
3370 .map = amd_iommu_map,
3371 .unmap = amd_iommu_unmap,
3372 .iova_to_phys = amd_iommu_iova_to_phys,
3373 .domain_has_cap = amd_iommu_domain_has_cap,
3395 ret = alloc_passthrough_domain();
3400 if (!check_device(&dev->
dev))
3403 dev_data = get_dev_data(&dev->
dev);
3406 devid = get_device_id(&dev->
dev);
3412 attach_device(&dev->
dev, pt_domain);
3415 amd_iommu_stats_init();
3417 pr_info(
"AMD-Vi: Initialized for Passthrough Mode\n");
3438 unsigned long flags;
3447 update_domain(domain);
3450 free_pagetable(domain);
3452 spin_unlock_irqrestore(&domain->
lock, flags);
3459 unsigned long flags;
3462 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3466 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3488 domain->
glx = levels;
3492 update_domain(domain);
3497 spin_unlock_irqrestore(&domain->
lock, flags);
3504 u64 address,
bool size)
3513 build_inv_iommu_pasid(&cmd, domain->
id, pasid, address, size);
3523 ret = iommu_queue_command(
amd_iommus[i], &cmd);
3529 domain_flush_complete(domain);
3538 qdep = dev_data->
ats.qdep;
3541 build_inv_iotlb_pasid(&cmd, dev_data->
devid, pasid,
3542 qdep, address, size);
3544 ret = iommu_queue_command(iommu, &cmd);
3550 domain_flush_complete(domain);
3564 return __flush_pasid(domain, pasid, address,
false);
3571 unsigned long flags;
3575 ret = __amd_iommu_flush_page(domain, pasid, address);
3576 spin_unlock_irqrestore(&domain->
lock, flags);
3593 unsigned long flags;
3597 ret = __amd_iommu_flush_tlb(domain, pasid);
3598 spin_unlock_irqrestore(&domain->
lock, flags);
3604 static u64 *__get_gcr3_pte(
u64 *root,
int level,
int pasid,
bool alloc)
3611 index = (pasid >> (9 *
level)) & 0x1ff;
3644 pte = __get_gcr3_pte(domain->
gcr3_tbl, domain->
glx, pasid,
true);
3650 return __amd_iommu_flush_tlb(domain, pasid);
3660 pte = __get_gcr3_pte(domain->
gcr3_tbl, domain->
glx, pasid,
false);
3666 return __amd_iommu_flush_tlb(domain, pasid);
3673 unsigned long flags;
3677 ret = __set_gcr3(domain, pasid, cr3);
3678 spin_unlock_irqrestore(&domain->
lock, flags);
3687 unsigned long flags;
3691 ret = __clear_gcr3(domain, pasid);
3692 spin_unlock_irqrestore(&domain->
lock, flags);
3699 int status,
int tag)
3707 dev_data = get_dev_data(&pdev->
dev);
3710 build_complete_ppr(&cmd, dev_data->
devid, pasid, status,
3713 return iommu_queue_command(iommu, &cmd);
3721 domain = get_domain(&pdev->
dev);
3740 dev_data = get_dev_data(&pdev->
dev);
3741 dev_data->
errata |= (1 << erratum);
3757 memset(info, 0,
sizeof(*info));
3761 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3765 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3772 max_pasids =
min(max_pasids, (1 << 20));
3774 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3775 info->max_pasids =
min(pci_max_pasids(pdev), max_pasids);
3777 features = pci_pasid_features(pdev);
3779 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3781 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3788 #ifdef CONFIG_IRQ_REMAP
3811 #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3812 #define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3813 #define DTE_IRQ_TABLE_LEN (8ULL << 1)
3814 #define DTE_IRQ_REMAP_ENABLE 1ULL
3821 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3823 dte |= DTE_IRQ_REMAP_INTCTL;
3824 dte |= DTE_IRQ_TABLE_LEN;
3825 dte |= DTE_IRQ_REMAP_ENABLE;
3830 #define IRTE_ALLOCATED (~1U)
3836 unsigned long flags;
3853 set_dte_irq_entry(devid, table);
3854 iommu_flush_dte(iommu, devid);
3868 if (!table->
table) {
3879 for (i = 0; i < 32; ++
i)
3880 table->
table[i] = IRTE_ALLOCATED;
3884 set_dte_irq_entry(devid, table);
3885 iommu_flush_dte(iommu, devid);
3886 if (devid != alias) {
3888 set_dte_irq_entry(devid, table);
3889 iommu_flush_dte(iommu, alias);
3893 iommu_completion_wait(iommu);
3901 static int alloc_irq_index(
struct irq_cfg *
cfg,
u16 devid,
int count)
3904 unsigned long flags;
3907 table = get_irq_table(devid,
false);
3917 if (table->
table[index] == 0)
3926 table->
table[index - c + 1] = IRTE_ALLOCATED;
3930 irte_info = &cfg->irq_2_iommu;
3942 spin_unlock_irqrestore(&table->
lock, flags);
3950 unsigned long flags;
3952 table = get_irq_table(devid,
false);
3958 spin_unlock_irqrestore(&table->
lock, flags);
3963 static int modify_irte(
u16 devid,
int index,
union irte irte)
3967 unsigned long flags;
3973 table = get_irq_table(devid,
false);
3979 spin_unlock_irqrestore(&table->
lock, flags);
3981 iommu_flush_irt(iommu, devid);
3982 iommu_completion_wait(iommu);
3987 static void free_irte(
u16 devid,
int index)
3991 unsigned long flags;
3997 table = get_irq_table(devid,
false);
4003 spin_unlock_irqrestore(&table->
lock, flags);
4005 iommu_flush_irt(iommu, devid);
4006 iommu_completion_wait(iommu);
4010 unsigned int destination,
int vector,
4022 cfg = irq_get_chip_data(irq);
4026 irte_info = &cfg->irq_2_iommu;
4028 devid = get_ioapic_devid(ioapic_id);
4033 table = get_irq_table(devid,
true);
4042 irte_info->
iommu = (
void *)cfg;
4048 irte.fields.destination = destination;
4050 irte.fields.valid = 1;
4052 ret = modify_irte(devid, index, irte);
4057 memset(entry, 0,
sizeof(*entry));
4077 unsigned int dest, irq;
4087 irte_info = &cfg->irq_2_iommu;
4089 if (!cpumask_intersects(mask, cpu_online_mask))
4101 pr_err(
"AMD-Vi: Failed to recover vector for irq %d\n", irq);
4106 irte.fields.destination =
dest;
4113 cpumask_copy(data->
affinity, mask);
4123 cfg = irq_get_chip_data(irq);
4127 irte_info = &cfg->irq_2_iommu;
4134 static void compose_msi_msg(
struct pci_dev *pdev,
4135 unsigned int irq,
unsigned int dest,
4142 cfg = irq_get_chip_data(irq);
4146 irte_info = &cfg->irq_2_iommu;
4149 irte.fields.
vector = cfg->vector;
4151 irte.fields.destination =
dest;
4153 irte.fields.valid = 1;
4162 static int msi_alloc_irq(
struct pci_dev *pdev,
int irq,
int nvec)
4171 cfg = irq_get_chip_data(irq);
4175 devid = get_device_id(&pdev->
dev);
4176 index = alloc_irq_index(cfg, devid, nvec);
4181 static int msi_setup_irq(
struct pci_dev *pdev,
unsigned int irq,
4182 int index,
int offset)
4191 cfg = irq_get_chip_data(irq);
4198 devid = get_device_id(&pdev->
dev);
4199 irte_info = &cfg->irq_2_iommu;
4203 irte_info->
iommu = (
void *)cfg;
4208 static int setup_hpet_msi(
unsigned int irq,
unsigned int id)
4214 cfg = irq_get_chip_data(irq);
4218 irte_info = &cfg->irq_2_iommu;
4219 devid = get_hpet_devid(
id);
4223 index = alloc_irq_index(cfg, devid, 1);
4229 irte_info->
iommu = (
void *)cfg;
4234 struct irq_remap_ops amd_iommu_irq_ops = {
4241 .setup_ioapic_entry = setup_ioapic_entry,
4242 .set_affinity = set_affinity,
4244 .compose_msi_msg = compose_msi_msg,
4245 .msi_alloc_irq = msi_alloc_irq,
4246 .msi_setup_irq = msi_setup_irq,
4247 .setup_hpet_msi = setup_hpet_msi,