|
| 1 | +#include <linux/types.h> |
| 2 | +#include <linux/kthread.h> |
| 3 | +#include <asm/xen/hypercall.h> |
| 4 | +#include <xen/interface/memory.h> |
| 5 | +#include <xen/hvc-console.h> |
| 6 | +#include <asm/xen/page.h> |
| 7 | + |
| 8 | +#include <xen/xen.h> |
| 9 | + |
| 10 | +#define IOMMU_BATCH_SIZE 128 |
| 11 | + |
| 12 | +extern unsigned long max_pfn; |
| 13 | +dma_addr_t pv_iommu_1_to_1_offset; |
| 14 | +EXPORT_SYMBOL(pv_iommu_1_to_1_offset); |
| 15 | + |
| 16 | +bool pv_iommu_1_to_1_setup_complete; |
| 17 | +EXPORT_SYMBOL(pv_iommu_1_to_1_setup_complete); |
| 18 | + |
| 19 | +static struct pv_iommu_op iommu_ops[IOMMU_BATCH_SIZE]; |
| 20 | + |
| 21 | +int xen_iommu_map_page(unsigned long bfn, unsigned long mfn) |
| 22 | +{ |
| 23 | + struct pv_iommu_op iommu_op; |
| 24 | + int rc; |
| 25 | + |
| 26 | + iommu_op.u.map_page.bfn = bfn; |
| 27 | + iommu_op.u.map_page.gfn = mfn; |
| 28 | + iommu_op.flags = IOMMU_OP_readable | IOMMU_OP_writeable | IOMMU_MAP_OP_no_ref_cnt; |
| 29 | + iommu_op.subop_id = IOMMUOP_map_page; |
| 30 | + rc = HYPERVISOR_iommu_op(&iommu_op, 1); |
| 31 | + if (rc < 0) { |
| 32 | + printk("Failed to setup IOMMU mapping for gpfn 0x%lx, mfn 0x%lx, err %d\n", |
| 33 | + bfn, mfn, rc); |
| 34 | + return rc; |
| 35 | + } |
| 36 | + return iommu_op.status; |
| 37 | +} |
| 38 | +EXPORT_SYMBOL_GPL(xen_iommu_map_page); |
| 39 | + |
| 40 | +int xen_iommu_unmap_page(unsigned long bfn) |
| 41 | +{ |
| 42 | + struct pv_iommu_op iommu_op; |
| 43 | + int rc; |
| 44 | + |
| 45 | + iommu_op.u.unmap_page.bfn = bfn; |
| 46 | + iommu_op.flags = IOMMU_MAP_OP_no_ref_cnt; |
| 47 | + iommu_op.subop_id = IOMMUOP_unmap_page; |
| 48 | + rc = HYPERVISOR_iommu_op(&iommu_op, 1); |
| 49 | + if (rc < 0) { |
| 50 | + printk("Failed to remove IOMMU mapping for gpfn 0x%lx, err %d\n", bfn, rc); |
| 51 | + return rc; |
| 52 | + } |
| 53 | + return iommu_op.status; |
| 54 | +} |
| 55 | + |
| 56 | +int xen_iommu_batch(struct pv_iommu_op *iommu_ops, int count) |
| 57 | +{ |
| 58 | + int rc; |
| 59 | + |
| 60 | + rc = HYPERVISOR_iommu_op(iommu_ops, count); |
| 61 | + if (rc < 0) { |
| 62 | + printk("Failed to batch IOMMU map, err %d\n", rc); |
| 63 | + } |
| 64 | + return rc; |
| 65 | +} |
| 66 | +EXPORT_SYMBOL_GPL(xen_iommu_batch); |
| 67 | + |
| 68 | +static int check_batch(int size) |
| 69 | +{ |
| 70 | + int op; |
| 71 | + int res=0; |
| 72 | + for (op = 1; op < size; op +=2) |
| 73 | + { |
| 74 | + if ( iommu_ops[op].status ) { |
| 75 | + printk("Iommu op %d went wrong, subop id %d, bfn 0x%llx, gfn 0x%llx\n, err %d, flags 0x%x\n", |
| 76 | + op, iommu_ops[op].subop_id, |
| 77 | + iommu_ops[op].u.map_page.bfn, |
| 78 | + iommu_ops[op].u.map_page.gfn, |
| 79 | + iommu_ops[op].status, iommu_ops[op].flags ); |
| 80 | + res++; |
| 81 | + } |
| 82 | + } |
| 83 | + return res; |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +void __init pci_xen_pv_iommu_late_init(void) |
| 88 | +{ |
| 89 | + if (pv_iommu_1_to_1_offset) { |
| 90 | + /* Xen has already set up 1-1 mapping for us */ |
| 91 | + pv_iommu_1_to_1_setup_complete = true; |
| 92 | + printk(KERN_INFO "XEN-PV-IOMMU - completed setting up 1-1 mapping\n"); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/* |
| 97 | + * Detect is we can use PV-IOMMU |
| 98 | + * If we can, caller needs to disable xen_swiotlb. |
| 99 | + * and additionally set up normal x86 swiotlb. (PV-IOMMU is layered on top) |
| 100 | + * Otherwise keep xen_swiotlb & don't setup x86 one. |
| 101 | + */ |
| 102 | + |
| 103 | +int pci_xen_swiotlb_pviommu_detect(void) |
| 104 | +{ |
| 105 | + int count = 0; |
| 106 | + u64 pfn, pfn_limit, max_host_mfn = 0; |
| 107 | + struct pv_iommu_op_ext iommu_op; |
| 108 | + int rc; |
| 109 | + |
| 110 | + if (!xen_initial_domain()) |
| 111 | + return 0; |
| 112 | + |
| 113 | + iommu_op.u.query_caps.offset = 0; |
| 114 | + iommu_op.flags = 0; |
| 115 | + iommu_op.status = 0; |
| 116 | + iommu_op.subop_id = IOMMUOP_query_caps; |
| 117 | + rc = HYPERVISOR_iommu_op(&iommu_op, 1); |
| 118 | + |
| 119 | + if (rc || !(iommu_op.flags & IOMMU_QUERY_map_cap)) { |
| 120 | + printk(KERN_INFO "XEN-PV-IOMMU: No IOMMU cap.\n"); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + max_host_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL); |
| 125 | + printk("Max host RAM MFN is 0x%llx\n",max_host_mfn); |
| 126 | + printk("max_pfn is 0x%lx\n",max_pfn); |
| 127 | + |
| 128 | + /* Check and Setup 1-1 host RAM offset location */ |
| 129 | + if (iommu_op.flags & IOMMU_QUERY_map_all_mfns) |
| 130 | + pv_iommu_1_to_1_offset = (dma_addr_t) iommu_op.u.query_caps.offset << PAGE_SHIFT; |
| 131 | + /* If offset is 0 or not set - disable PV IOMMU */ |
| 132 | + if (!pv_iommu_1_to_1_offset) { |
| 133 | + printk(KERN_INFO "XEN-PV-IOMMU: Disabled.\n"); |
| 134 | + return 0; |
| 135 | + } |
| 136 | + pfn_limit = pv_iommu_1_to_1_offset >> PAGE_SHIFT; |
| 137 | + if (max_pfn >= pfn_limit) { |
| 138 | + printk("XEN-PV-IOMMU: bfn_foreign_offset at %llu, is too small" |
| 139 | + " for Dom0. Needs %lu\n", pfn_limit, max_pfn + 1); |
| 140 | + BUG(); |
| 141 | + } |
| 142 | + |
| 143 | + pfn_limit = min(max_host_mfn, pfn_limit); |
| 144 | + |
| 145 | + /* Setup 1-1 mapping of GPFN to MFN */ |
| 146 | + for (pfn=0; pfn < pfn_limit; pfn++) |
| 147 | + { |
| 148 | + unsigned long mfn = get_phys_to_machine(pfn); |
| 149 | + if (mfn != INVALID_P2M_ENTRY && mfn != IDENTITY_FRAME(pfn)) |
| 150 | + { |
| 151 | + iommu_ops[count].u.unmap_page.bfn = pfn; |
| 152 | + iommu_ops[count].flags = IOMMU_MAP_OP_no_ref_cnt; |
| 153 | + iommu_ops[count].subop_id = IOMMUOP_unmap_page; |
| 154 | + count++; |
| 155 | + iommu_ops[count].u.map_page.bfn = pfn; |
| 156 | + iommu_ops[count].u.map_page.gfn = pfn_to_mfn(pfn); |
| 157 | + iommu_ops[count].flags = IOMMU_OP_readable | |
| 158 | + IOMMU_OP_writeable | |
| 159 | + IOMMU_MAP_OP_no_ref_cnt; |
| 160 | + iommu_ops[count].subop_id = IOMMUOP_map_page; |
| 161 | + count++; |
| 162 | + } |
| 163 | + if (count == IOMMU_BATCH_SIZE) |
| 164 | + { |
| 165 | + count = 0; |
| 166 | + if (xen_iommu_batch(iommu_ops, IOMMU_BATCH_SIZE) |
| 167 | + || check_batch(IOMMU_BATCH_SIZE)) { |
| 168 | + printk("Failed to fully Setup 1-1 mapping of GPFN to MFN.\n"); |
| 169 | + BUG(); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + if (count) { |
| 174 | + if (xen_iommu_batch(iommu_ops, count) |
| 175 | + || check_batch(count)) { |
| 176 | + printk("Failed to fully Setup 1-1 mapping of GPFN to MFN.\n"); |
| 177 | + BUG(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + printk("XEN-PV-IOMMU: Using GPFN IOMMU mode, 1-to-1 offset is 0x%llx\n", |
| 182 | + pv_iommu_1_to_1_offset); |
| 183 | + return 1; |
| 184 | +} |
0 commit comments