1 // SPDX-License-Identifier: GPL-2.0
3 * IOMMU API for s390 PCI devices
5 * Copyright IBM Corp. 2015
6 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
10 #include <linux/iommu.h>
11 #include <linux/iommu-helper.h>
12 #include <linux/sizes.h>
13 #include <asm/pci_dma.h>
16 * Physically contiguous memory regions can be mapped with 4 KiB alignment,
17 * we allow all page sizes that are an order of 4KiB (no special large page
20 #define S390_IOMMU_PGSIZES (~0xFFFUL)
22 static const struct iommu_ops s390_iommu_ops;
25 struct iommu_domain domain;
26 struct list_head devices;
27 unsigned long *dma_table;
28 spinlock_t dma_table_lock;
32 struct s390_domain_device {
33 struct list_head list;
34 struct zpci_dev *zdev;
37 static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
39 return container_of(dom, struct s390_domain, domain);
42 static bool s390_iommu_capable(enum iommu_cap cap)
45 case IOMMU_CAP_CACHE_COHERENCY:
47 case IOMMU_CAP_INTR_REMAP:
54 static struct iommu_domain *s390_domain_alloc(unsigned domain_type)
56 struct s390_domain *s390_domain;
58 if (domain_type != IOMMU_DOMAIN_UNMANAGED)
61 s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
65 s390_domain->dma_table = dma_alloc_cpu_table();
66 if (!s390_domain->dma_table) {
71 spin_lock_init(&s390_domain->dma_table_lock);
72 spin_lock_init(&s390_domain->list_lock);
73 INIT_LIST_HEAD(&s390_domain->devices);
75 return &s390_domain->domain;
78 static void s390_domain_free(struct iommu_domain *domain)
80 struct s390_domain *s390_domain = to_s390_domain(domain);
82 dma_cleanup_tables(s390_domain->dma_table);
86 static int s390_iommu_attach_device(struct iommu_domain *domain,
89 struct s390_domain *s390_domain = to_s390_domain(domain);
90 struct zpci_dev *zdev = to_zpci_dev(dev);
91 struct s390_domain_device *domain_device;
98 domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL);
102 if (zdev->dma_table) {
103 cc = zpci_dma_exit_device(zdev);
110 zdev->dma_table = s390_domain->dma_table;
111 cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
112 (u64) zdev->dma_table);
118 spin_lock_irqsave(&s390_domain->list_lock, flags);
119 /* First device defines the DMA range limits */
120 if (list_empty(&s390_domain->devices)) {
121 domain->geometry.aperture_start = zdev->start_dma;
122 domain->geometry.aperture_end = zdev->end_dma;
123 domain->geometry.force_aperture = true;
124 /* Allow only devices with identical DMA range limits */
125 } else if (domain->geometry.aperture_start != zdev->start_dma ||
126 domain->geometry.aperture_end != zdev->end_dma) {
128 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
131 domain_device->zdev = zdev;
132 zdev->s390_domain = s390_domain;
133 list_add(&domain_device->list, &s390_domain->devices);
134 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
139 zpci_dma_init_device(zdev);
141 kfree(domain_device);
146 static void s390_iommu_detach_device(struct iommu_domain *domain,
149 struct s390_domain *s390_domain = to_s390_domain(domain);
150 struct zpci_dev *zdev = to_zpci_dev(dev);
151 struct s390_domain_device *domain_device, *tmp;
158 spin_lock_irqsave(&s390_domain->list_lock, flags);
159 list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices,
161 if (domain_device->zdev == zdev) {
162 list_del(&domain_device->list);
163 kfree(domain_device);
168 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
171 zdev->s390_domain = NULL;
172 zpci_unregister_ioat(zdev, 0);
173 zpci_dma_init_device(zdev);
177 static struct iommu_device *s390_iommu_probe_device(struct device *dev)
179 struct zpci_dev *zdev = to_zpci_dev(dev);
181 return &zdev->iommu_dev;
184 static void s390_iommu_release_device(struct device *dev)
186 struct zpci_dev *zdev = to_zpci_dev(dev);
187 struct iommu_domain *domain;
190 * This is a workaround for a scenario where the IOMMU API common code
191 * "forgets" to call the detach_dev callback: After binding a device
192 * to vfio-pci and completing the VFIO_SET_IOMMU ioctl (which triggers
193 * the attach_dev), removing the device via
194 * "echo 1 > /sys/bus/pci/devices/.../remove" won't trigger detach_dev,
195 * only release_device will be called via the BUS_NOTIFY_REMOVED_DEVICE
198 * So let's call detach_dev from here if it hasn't been called before.
200 if (zdev && zdev->s390_domain) {
201 domain = iommu_get_domain_for_dev(dev);
203 s390_iommu_detach_device(domain, dev);
207 static int s390_iommu_update_trans(struct s390_domain *s390_domain,
208 unsigned long pa, dma_addr_t dma_addr,
209 size_t size, int flags)
211 struct s390_domain_device *domain_device;
212 u8 *page_addr = (u8 *) (pa & PAGE_MASK);
213 dma_addr_t start_dma_addr = dma_addr;
214 unsigned long irq_flags, nr_pages, i;
215 unsigned long *entry;
218 if (dma_addr < s390_domain->domain.geometry.aperture_start ||
219 dma_addr + size > s390_domain->domain.geometry.aperture_end)
222 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
226 spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags);
227 for (i = 0; i < nr_pages; i++) {
228 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
233 dma_update_cpu_trans(entry, page_addr, flags);
234 page_addr += PAGE_SIZE;
235 dma_addr += PAGE_SIZE;
238 spin_lock(&s390_domain->list_lock);
239 list_for_each_entry(domain_device, &s390_domain->devices, list) {
240 rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32,
241 start_dma_addr, nr_pages * PAGE_SIZE);
245 spin_unlock(&s390_domain->list_lock);
248 if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
249 flags = ZPCI_PTE_INVALID;
251 page_addr -= PAGE_SIZE;
252 dma_addr -= PAGE_SIZE;
253 entry = dma_walk_cpu_trans(s390_domain->dma_table,
257 dma_update_cpu_trans(entry, page_addr, flags);
260 spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags);
265 static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
266 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
268 struct s390_domain *s390_domain = to_s390_domain(domain);
269 int flags = ZPCI_PTE_VALID, rc = 0;
271 if (!(prot & IOMMU_READ))
274 if (!(prot & IOMMU_WRITE))
275 flags |= ZPCI_TABLE_PROTECTED;
277 rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
283 static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
286 struct s390_domain *s390_domain = to_s390_domain(domain);
287 unsigned long *sto, *pto, *rto, flags;
288 unsigned int rtx, sx, px;
289 phys_addr_t phys = 0;
291 if (iova < domain->geometry.aperture_start ||
292 iova > domain->geometry.aperture_end)
295 rtx = calc_rtx(iova);
298 rto = s390_domain->dma_table;
300 spin_lock_irqsave(&s390_domain->dma_table_lock, flags);
301 if (rto && reg_entry_isvalid(rto[rtx])) {
302 sto = get_rt_sto(rto[rtx]);
303 if (sto && reg_entry_isvalid(sto[sx])) {
304 pto = get_st_pto(sto[sx]);
305 if (pto && pt_entry_isvalid(pto[px]))
306 phys = pto[px] & ZPCI_PTE_ADDR_MASK;
309 spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags);
314 static size_t s390_iommu_unmap(struct iommu_domain *domain,
315 unsigned long iova, size_t size,
316 struct iommu_iotlb_gather *gather)
318 struct s390_domain *s390_domain = to_s390_domain(domain);
319 int flags = ZPCI_PTE_INVALID;
323 paddr = s390_iommu_iova_to_phys(domain, iova);
327 rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
335 int zpci_init_iommu(struct zpci_dev *zdev)
339 rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
340 "s390-iommu.%08x", zdev->fid);
344 rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
351 iommu_device_sysfs_remove(&zdev->iommu_dev);
357 void zpci_destroy_iommu(struct zpci_dev *zdev)
359 iommu_device_unregister(&zdev->iommu_dev);
360 iommu_device_sysfs_remove(&zdev->iommu_dev);
363 static const struct iommu_ops s390_iommu_ops = {
364 .capable = s390_iommu_capable,
365 .domain_alloc = s390_domain_alloc,
366 .domain_free = s390_domain_free,
367 .attach_dev = s390_iommu_attach_device,
368 .detach_dev = s390_iommu_detach_device,
369 .map = s390_iommu_map,
370 .unmap = s390_iommu_unmap,
371 .iova_to_phys = s390_iommu_iova_to_phys,
372 .probe_device = s390_iommu_probe_device,
373 .release_device = s390_iommu_release_device,
374 .device_group = generic_device_group,
375 .pgsize_bitmap = S390_IOMMU_PGSIZES,
378 static int __init s390_iommu_init(void)
380 return bus_set_iommu(&pci_bus_type, &s390_iommu_ops);
382 subsys_initcall(s390_iommu_init);