1 /* 2 * IOMMU API for s390 PCI devices 3 * 4 * Copyright IBM Corp. 2015 5 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com> 6 */ 7 8 #include <linux/pci.h> 9 #include <linux/iommu.h> 10 #include <linux/iommu-helper.h> 11 #include <linux/sizes.h> 12 #include <asm/pci_dma.h> 13 14 /* 15 * Physically contiguous memory regions can be mapped with 4 KiB alignment, 16 * we allow all page sizes that are an order of 4KiB (no special large page 17 * support so far). 18 */ 19 #define S390_IOMMU_PGSIZES (~0xFFFUL) 20 21 static const struct iommu_ops s390_iommu_ops; 22 23 struct s390_domain { 24 struct iommu_domain domain; 25 struct list_head devices; 26 unsigned long *dma_table; 27 spinlock_t dma_table_lock; 28 spinlock_t list_lock; 29 }; 30 31 struct s390_domain_device { 32 struct list_head list; 33 struct zpci_dev *zdev; 34 }; 35 36 static struct s390_domain *to_s390_domain(struct iommu_domain *dom) 37 { 38 return container_of(dom, struct s390_domain, domain); 39 } 40 41 static bool s390_iommu_capable(enum iommu_cap cap) 42 { 43 switch (cap) { 44 case IOMMU_CAP_CACHE_COHERENCY: 45 return true; 46 case IOMMU_CAP_INTR_REMAP: 47 return true; 48 default: 49 return false; 50 } 51 } 52 53 static struct iommu_domain *s390_domain_alloc(unsigned domain_type) 54 { 55 struct s390_domain *s390_domain; 56 57 if (domain_type != IOMMU_DOMAIN_UNMANAGED) 58 return NULL; 59 60 s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL); 61 if (!s390_domain) 62 return NULL; 63 64 s390_domain->dma_table = dma_alloc_cpu_table(); 65 if (!s390_domain->dma_table) { 66 kfree(s390_domain); 67 return NULL; 68 } 69 70 spin_lock_init(&s390_domain->dma_table_lock); 71 spin_lock_init(&s390_domain->list_lock); 72 INIT_LIST_HEAD(&s390_domain->devices); 73 74 return &s390_domain->domain; 75 } 76 77 static void s390_domain_free(struct iommu_domain *domain) 78 { 79 struct s390_domain *s390_domain = to_s390_domain(domain); 80 81 dma_cleanup_tables(s390_domain->dma_table); 82 kfree(s390_domain); 83 } 84 85 static int s390_iommu_attach_device(struct iommu_domain *domain, 86 struct device *dev) 87 { 88 struct s390_domain *s390_domain = to_s390_domain(domain); 89 struct zpci_dev *zdev = to_pci_dev(dev)->sysdata; 90 struct s390_domain_device *domain_device; 91 unsigned long flags; 92 int rc; 93 94 if (!zdev) 95 return -ENODEV; 96 97 domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL); 98 if (!domain_device) 99 return -ENOMEM; 100 101 if (zdev->dma_table) 102 zpci_dma_exit_device(zdev); 103 104 zdev->dma_table = s390_domain->dma_table; 105 rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 106 (u64) zdev->dma_table); 107 if (rc) 108 goto out_restore; 109 110 spin_lock_irqsave(&s390_domain->list_lock, flags); 111 /* First device defines the DMA range limits */ 112 if (list_empty(&s390_domain->devices)) { 113 domain->geometry.aperture_start = zdev->start_dma; 114 domain->geometry.aperture_end = zdev->end_dma; 115 domain->geometry.force_aperture = true; 116 /* Allow only devices with identical DMA range limits */ 117 } else if (domain->geometry.aperture_start != zdev->start_dma || 118 domain->geometry.aperture_end != zdev->end_dma) { 119 rc = -EINVAL; 120 spin_unlock_irqrestore(&s390_domain->list_lock, flags); 121 goto out_restore; 122 } 123 domain_device->zdev = zdev; 124 zdev->s390_domain = s390_domain; 125 list_add(&domain_device->list, &s390_domain->devices); 126 spin_unlock_irqrestore(&s390_domain->list_lock, flags); 127 128 return 0; 129 130 out_restore: 131 zpci_dma_init_device(zdev); 132 kfree(domain_device); 133 134 return rc; 135 } 136 137 static void s390_iommu_detach_device(struct iommu_domain *domain, 138 struct device *dev) 139 { 140 struct s390_domain *s390_domain = to_s390_domain(domain); 141 struct zpci_dev *zdev = to_pci_dev(dev)->sysdata; 142 struct s390_domain_device *domain_device, *tmp; 143 unsigned long flags; 144 int found = 0; 145 146 if (!zdev) 147 return; 148 149 spin_lock_irqsave(&s390_domain->list_lock, flags); 150 list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices, 151 list) { 152 if (domain_device->zdev == zdev) { 153 list_del(&domain_device->list); 154 kfree(domain_device); 155 found = 1; 156 break; 157 } 158 } 159 spin_unlock_irqrestore(&s390_domain->list_lock, flags); 160 161 if (found) { 162 zdev->s390_domain = NULL; 163 zpci_unregister_ioat(zdev, 0); 164 zpci_dma_init_device(zdev); 165 } 166 } 167 168 static int s390_iommu_add_device(struct device *dev) 169 { 170 struct iommu_group *group = iommu_group_get_for_dev(dev); 171 struct zpci_dev *zdev = to_pci_dev(dev)->sysdata; 172 173 if (IS_ERR(group)) 174 return PTR_ERR(group); 175 176 iommu_group_put(group); 177 iommu_device_link(&zdev->iommu_dev, dev); 178 179 return 0; 180 } 181 182 static void s390_iommu_remove_device(struct device *dev) 183 { 184 struct zpci_dev *zdev = to_pci_dev(dev)->sysdata; 185 struct iommu_domain *domain; 186 187 /* 188 * This is a workaround for a scenario where the IOMMU API common code 189 * "forgets" to call the detach_dev callback: After binding a device 190 * to vfio-pci and completing the VFIO_SET_IOMMU ioctl (which triggers 191 * the attach_dev), removing the device via 192 * "echo 1 > /sys/bus/pci/devices/.../remove" won't trigger detach_dev, 193 * only remove_device will be called via the BUS_NOTIFY_REMOVED_DEVICE 194 * notifier. 195 * 196 * So let's call detach_dev from here if it hasn't been called before. 197 */ 198 if (zdev && zdev->s390_domain) { 199 domain = iommu_get_domain_for_dev(dev); 200 if (domain) 201 s390_iommu_detach_device(domain, dev); 202 } 203 204 iommu_device_unlink(&zdev->iommu_dev, dev); 205 iommu_group_remove_device(dev); 206 } 207 208 static int s390_iommu_update_trans(struct s390_domain *s390_domain, 209 unsigned long pa, dma_addr_t dma_addr, 210 size_t size, int flags) 211 { 212 struct s390_domain_device *domain_device; 213 u8 *page_addr = (u8 *) (pa & PAGE_MASK); 214 dma_addr_t start_dma_addr = dma_addr; 215 unsigned long irq_flags, nr_pages, i; 216 unsigned long *entry; 217 int rc = 0; 218 219 if (dma_addr < s390_domain->domain.geometry.aperture_start || 220 dma_addr + size > s390_domain->domain.geometry.aperture_end) 221 return -EINVAL; 222 223 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; 224 if (!nr_pages) 225 return 0; 226 227 spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags); 228 for (i = 0; i < nr_pages; i++) { 229 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr); 230 if (!entry) { 231 rc = -ENOMEM; 232 goto undo_cpu_trans; 233 } 234 dma_update_cpu_trans(entry, page_addr, flags); 235 page_addr += PAGE_SIZE; 236 dma_addr += PAGE_SIZE; 237 } 238 239 spin_lock(&s390_domain->list_lock); 240 list_for_each_entry(domain_device, &s390_domain->devices, list) { 241 rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32, 242 start_dma_addr, nr_pages * PAGE_SIZE); 243 if (rc) 244 break; 245 } 246 spin_unlock(&s390_domain->list_lock); 247 248 undo_cpu_trans: 249 if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) { 250 flags = ZPCI_PTE_INVALID; 251 while (i-- > 0) { 252 page_addr -= PAGE_SIZE; 253 dma_addr -= PAGE_SIZE; 254 entry = dma_walk_cpu_trans(s390_domain->dma_table, 255 dma_addr); 256 if (!entry) 257 break; 258 dma_update_cpu_trans(entry, page_addr, flags); 259 } 260 } 261 spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags); 262 263 return rc; 264 } 265 266 static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova, 267 phys_addr_t paddr, size_t size, int prot) 268 { 269 struct s390_domain *s390_domain = to_s390_domain(domain); 270 int flags = ZPCI_PTE_VALID, rc = 0; 271 272 if (!(prot & IOMMU_READ)) 273 return -EINVAL; 274 275 if (!(prot & IOMMU_WRITE)) 276 flags |= ZPCI_TABLE_PROTECTED; 277 278 rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova, 279 size, flags); 280 281 return rc; 282 } 283 284 static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain, 285 dma_addr_t iova) 286 { 287 struct s390_domain *s390_domain = to_s390_domain(domain); 288 unsigned long *sto, *pto, *rto, flags; 289 unsigned int rtx, sx, px; 290 phys_addr_t phys = 0; 291 292 if (iova < domain->geometry.aperture_start || 293 iova > domain->geometry.aperture_end) 294 return 0; 295 296 rtx = calc_rtx(iova); 297 sx = calc_sx(iova); 298 px = calc_px(iova); 299 rto = s390_domain->dma_table; 300 301 spin_lock_irqsave(&s390_domain->dma_table_lock, flags); 302 if (rto && reg_entry_isvalid(rto[rtx])) { 303 sto = get_rt_sto(rto[rtx]); 304 if (sto && reg_entry_isvalid(sto[sx])) { 305 pto = get_st_pto(sto[sx]); 306 if (pto && pt_entry_isvalid(pto[px])) 307 phys = pto[px] & ZPCI_PTE_ADDR_MASK; 308 } 309 } 310 spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags); 311 312 return phys; 313 } 314 315 static size_t s390_iommu_unmap(struct iommu_domain *domain, 316 unsigned long iova, size_t size) 317 { 318 struct s390_domain *s390_domain = to_s390_domain(domain); 319 int flags = ZPCI_PTE_INVALID; 320 phys_addr_t paddr; 321 int rc; 322 323 paddr = s390_iommu_iova_to_phys(domain, iova); 324 if (!paddr) 325 return 0; 326 327 rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova, 328 size, flags); 329 if (rc) 330 return 0; 331 332 return size; 333 } 334 335 int zpci_init_iommu(struct zpci_dev *zdev) 336 { 337 int rc = 0; 338 339 rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL, 340 "s390-iommu.%08x", zdev->fid); 341 if (rc) 342 goto out_err; 343 344 iommu_device_set_ops(&zdev->iommu_dev, &s390_iommu_ops); 345 346 rc = iommu_device_register(&zdev->iommu_dev); 347 if (rc) 348 goto out_sysfs; 349 350 return 0; 351 352 out_sysfs: 353 iommu_device_sysfs_remove(&zdev->iommu_dev); 354 355 out_err: 356 return rc; 357 } 358 359 void zpci_destroy_iommu(struct zpci_dev *zdev) 360 { 361 iommu_device_unregister(&zdev->iommu_dev); 362 iommu_device_sysfs_remove(&zdev->iommu_dev); 363 } 364 365 static const struct iommu_ops s390_iommu_ops = { 366 .capable = s390_iommu_capable, 367 .domain_alloc = s390_domain_alloc, 368 .domain_free = s390_domain_free, 369 .attach_dev = s390_iommu_attach_device, 370 .detach_dev = s390_iommu_detach_device, 371 .map = s390_iommu_map, 372 .unmap = s390_iommu_unmap, 373 .iova_to_phys = s390_iommu_iova_to_phys, 374 .add_device = s390_iommu_add_device, 375 .remove_device = s390_iommu_remove_device, 376 .device_group = generic_device_group, 377 .pgsize_bitmap = S390_IOMMU_PGSIZES, 378 }; 379 380 static int __init s390_iommu_init(void) 381 { 382 return bus_set_iommu(&pci_bus_type, &s390_iommu_ops); 383 } 384 subsys_initcall(s390_iommu_init); 385