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(struct device *dev, 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 WARN_ON(!list_empty(&s390_domain->devices));
83 dma_cleanup_tables(s390_domain->dma_table);
87 static void __s390_iommu_detach_device(struct zpci_dev *zdev)
89 struct s390_domain *s390_domain = zdev->s390_domain;
90 struct s390_domain_device *domain_device, *tmp;
96 spin_lock_irqsave(&s390_domain->list_lock, flags);
97 list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices,
99 if (domain_device->zdev == zdev) {
100 list_del(&domain_device->list);
101 kfree(domain_device);
105 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
107 zpci_unregister_ioat(zdev, 0);
108 zdev->s390_domain = NULL;
109 zdev->dma_table = NULL;
112 static int s390_iommu_attach_device(struct iommu_domain *domain,
115 struct s390_domain *s390_domain = to_s390_domain(domain);
116 struct zpci_dev *zdev = to_zpci_dev(dev);
117 struct s390_domain_device *domain_device;
124 domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL);
128 if (zdev->s390_domain)
129 __s390_iommu_detach_device(zdev);
130 else if (zdev->dma_table)
131 zpci_dma_exit_device(zdev);
133 cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
134 virt_to_phys(s390_domain->dma_table));
139 zdev->dma_table = s390_domain->dma_table;
141 spin_lock_irqsave(&s390_domain->list_lock, flags);
142 /* First device defines the DMA range limits */
143 if (list_empty(&s390_domain->devices)) {
144 domain->geometry.aperture_start = zdev->start_dma;
145 domain->geometry.aperture_end = zdev->end_dma;
146 domain->geometry.force_aperture = true;
147 /* Allow only devices with identical DMA range limits */
148 } else if (domain->geometry.aperture_start != zdev->start_dma ||
149 domain->geometry.aperture_end != zdev->end_dma) {
150 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
154 domain_device->zdev = zdev;
155 zdev->s390_domain = s390_domain;
156 list_add(&domain_device->list, &s390_domain->devices);
157 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
162 zpci_unregister_ioat(zdev, 0);
163 zdev->dma_table = NULL;
165 kfree(domain_device);
170 static void s390_iommu_detach_device(struct iommu_domain *domain,
173 struct zpci_dev *zdev = to_zpci_dev(dev);
175 WARN_ON(zdev->s390_domain != to_s390_domain(domain));
177 __s390_iommu_detach_device(zdev);
178 zpci_dma_init_device(zdev);
181 static struct iommu_device *s390_iommu_probe_device(struct device *dev)
183 struct zpci_dev *zdev;
185 if (!dev_is_pci(dev))
186 return ERR_PTR(-ENODEV);
188 zdev = to_zpci_dev(dev);
190 return &zdev->iommu_dev;
193 static void s390_iommu_release_device(struct device *dev)
195 struct zpci_dev *zdev = to_zpci_dev(dev);
198 * release_device is expected to detach any domain currently attached
199 * to the device, but keep it attached to other devices in the group.
202 __s390_iommu_detach_device(zdev);
205 static int s390_iommu_update_trans(struct s390_domain *s390_domain,
206 phys_addr_t pa, dma_addr_t dma_addr,
207 size_t size, int flags)
209 struct s390_domain_device *domain_device;
210 phys_addr_t page_addr = pa & PAGE_MASK;
211 dma_addr_t start_dma_addr = dma_addr;
212 unsigned long irq_flags, nr_pages, i;
213 unsigned long *entry;
216 if (dma_addr < s390_domain->domain.geometry.aperture_start ||
217 dma_addr + size > s390_domain->domain.geometry.aperture_end)
220 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
224 spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags);
225 for (i = 0; i < nr_pages; i++) {
226 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
231 dma_update_cpu_trans(entry, page_addr, flags);
232 page_addr += PAGE_SIZE;
233 dma_addr += PAGE_SIZE;
236 spin_lock(&s390_domain->list_lock);
237 list_for_each_entry(domain_device, &s390_domain->devices, list) {
238 rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32,
239 start_dma_addr, nr_pages * PAGE_SIZE);
243 spin_unlock(&s390_domain->list_lock);
246 if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
247 flags = ZPCI_PTE_INVALID;
249 page_addr -= PAGE_SIZE;
250 dma_addr -= PAGE_SIZE;
251 entry = dma_walk_cpu_trans(s390_domain->dma_table,
255 dma_update_cpu_trans(entry, page_addr, flags);
258 spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags);
263 static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
264 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
266 struct s390_domain *s390_domain = to_s390_domain(domain);
267 int flags = ZPCI_PTE_VALID, rc = 0;
269 if (!(prot & IOMMU_READ))
272 if (!(prot & IOMMU_WRITE))
273 flags |= ZPCI_TABLE_PROTECTED;
275 rc = s390_iommu_update_trans(s390_domain, paddr, iova,
281 static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
284 struct s390_domain *s390_domain = to_s390_domain(domain);
285 unsigned long *sto, *pto, *rto, flags;
286 unsigned int rtx, sx, px;
287 phys_addr_t phys = 0;
289 if (iova < domain->geometry.aperture_start ||
290 iova > domain->geometry.aperture_end)
293 rtx = calc_rtx(iova);
296 rto = s390_domain->dma_table;
298 spin_lock_irqsave(&s390_domain->dma_table_lock, flags);
299 if (rto && reg_entry_isvalid(rto[rtx])) {
300 sto = get_rt_sto(rto[rtx]);
301 if (sto && reg_entry_isvalid(sto[sx])) {
302 pto = get_st_pto(sto[sx]);
303 if (pto && pt_entry_isvalid(pto[px]))
304 phys = pto[px] & ZPCI_PTE_ADDR_MASK;
307 spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags);
312 static size_t s390_iommu_unmap(struct iommu_domain *domain,
313 unsigned long iova, size_t size,
314 struct iommu_iotlb_gather *gather)
316 struct s390_domain *s390_domain = to_s390_domain(domain);
317 int flags = ZPCI_PTE_INVALID;
321 paddr = s390_iommu_iova_to_phys(domain, iova);
325 rc = s390_iommu_update_trans(s390_domain, paddr, iova,
333 int zpci_init_iommu(struct zpci_dev *zdev)
337 rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
338 "s390-iommu.%08x", zdev->fid);
342 rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
349 iommu_device_sysfs_remove(&zdev->iommu_dev);
355 void zpci_destroy_iommu(struct zpci_dev *zdev)
357 iommu_device_unregister(&zdev->iommu_dev);
358 iommu_device_sysfs_remove(&zdev->iommu_dev);
361 static const struct iommu_ops s390_iommu_ops = {
362 .capable = s390_iommu_capable,
363 .domain_alloc = s390_domain_alloc,
364 .probe_device = s390_iommu_probe_device,
365 .release_device = s390_iommu_release_device,
366 .device_group = generic_device_group,
367 .pgsize_bitmap = S390_IOMMU_PGSIZES,
368 .default_domain_ops = &(const struct iommu_domain_ops) {
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 .free = s390_domain_free,