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 <linux/rculist.h>
14 #include <linux/rcupdate.h>
15 #include <asm/pci_dma.h>
17 static const struct iommu_ops s390_iommu_ops;
20 struct iommu_domain domain;
21 struct list_head devices;
22 unsigned long *dma_table;
27 static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
29 return container_of(dom, struct s390_domain, domain);
32 static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap)
35 case IOMMU_CAP_CACHE_COHERENCY:
42 static struct iommu_domain *s390_domain_alloc(unsigned domain_type)
44 struct s390_domain *s390_domain;
46 if (domain_type != IOMMU_DOMAIN_UNMANAGED)
49 s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
53 s390_domain->dma_table = dma_alloc_cpu_table(GFP_KERNEL);
54 if (!s390_domain->dma_table) {
58 s390_domain->domain.geometry.force_aperture = true;
59 s390_domain->domain.geometry.aperture_start = 0;
60 s390_domain->domain.geometry.aperture_end = ZPCI_TABLE_SIZE_RT - 1;
62 spin_lock_init(&s390_domain->list_lock);
63 INIT_LIST_HEAD_RCU(&s390_domain->devices);
65 return &s390_domain->domain;
68 static void s390_iommu_rcu_free_domain(struct rcu_head *head)
70 struct s390_domain *s390_domain = container_of(head, struct s390_domain, rcu);
72 dma_cleanup_tables(s390_domain->dma_table);
76 static void s390_domain_free(struct iommu_domain *domain)
78 struct s390_domain *s390_domain = to_s390_domain(domain);
81 WARN_ON(!list_empty(&s390_domain->devices));
84 call_rcu(&s390_domain->rcu, s390_iommu_rcu_free_domain);
87 static void __s390_iommu_detach_device(struct zpci_dev *zdev)
89 struct s390_domain *s390_domain = zdev->s390_domain;
95 spin_lock_irqsave(&s390_domain->list_lock, flags);
96 list_del_rcu(&zdev->iommu_list);
97 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
99 zpci_unregister_ioat(zdev, 0);
100 zdev->s390_domain = NULL;
101 zdev->dma_table = NULL;
104 static int s390_iommu_attach_device(struct iommu_domain *domain,
107 struct s390_domain *s390_domain = to_s390_domain(domain);
108 struct zpci_dev *zdev = to_zpci_dev(dev);
116 if (WARN_ON(domain->geometry.aperture_start > zdev->end_dma ||
117 domain->geometry.aperture_end < zdev->start_dma))
120 if (zdev->s390_domain)
121 __s390_iommu_detach_device(zdev);
122 else if (zdev->dma_table)
123 zpci_dma_exit_device(zdev);
125 cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
126 virt_to_phys(s390_domain->dma_table), &status);
128 * If the device is undergoing error recovery the reset code
129 * will re-establish the new domain.
131 if (cc && status != ZPCI_PCI_ST_FUNC_NOT_AVAIL)
133 zdev->dma_table = s390_domain->dma_table;
135 zdev->dma_table = s390_domain->dma_table;
136 zdev->s390_domain = s390_domain;
138 spin_lock_irqsave(&s390_domain->list_lock, flags);
139 list_add_rcu(&zdev->iommu_list, &s390_domain->devices);
140 spin_unlock_irqrestore(&s390_domain->list_lock, flags);
145 static void s390_iommu_set_platform_dma(struct device *dev)
147 struct zpci_dev *zdev = to_zpci_dev(dev);
149 __s390_iommu_detach_device(zdev);
150 zpci_dma_init_device(zdev);
153 static void s390_iommu_get_resv_regions(struct device *dev,
154 struct list_head *list)
156 struct zpci_dev *zdev = to_zpci_dev(dev);
157 struct iommu_resv_region *region;
159 if (zdev->start_dma) {
160 region = iommu_alloc_resv_region(0, zdev->start_dma, 0,
161 IOMMU_RESV_RESERVED, GFP_KERNEL);
164 list_add_tail(®ion->list, list);
167 if (zdev->end_dma < ZPCI_TABLE_SIZE_RT - 1) {
168 region = iommu_alloc_resv_region(zdev->end_dma + 1,
169 ZPCI_TABLE_SIZE_RT - zdev->end_dma - 1,
170 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
173 list_add_tail(®ion->list, list);
177 static struct iommu_device *s390_iommu_probe_device(struct device *dev)
179 struct zpci_dev *zdev;
181 if (!dev_is_pci(dev))
182 return ERR_PTR(-ENODEV);
184 zdev = to_zpci_dev(dev);
186 if (zdev->start_dma > zdev->end_dma ||
187 zdev->start_dma > ZPCI_TABLE_SIZE_RT - 1)
188 return ERR_PTR(-EINVAL);
190 if (zdev->end_dma > ZPCI_TABLE_SIZE_RT - 1)
191 zdev->end_dma = ZPCI_TABLE_SIZE_RT - 1;
193 return &zdev->iommu_dev;
196 static void s390_iommu_release_device(struct device *dev)
198 struct zpci_dev *zdev = to_zpci_dev(dev);
201 * release_device is expected to detach any domain currently attached
202 * to the device, but keep it attached to other devices in the group.
205 __s390_iommu_detach_device(zdev);
208 static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain)
210 struct s390_domain *s390_domain = to_s390_domain(domain);
211 struct zpci_dev *zdev;
214 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
215 zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma,
216 zdev->end_dma - zdev->start_dma + 1);
221 static void s390_iommu_iotlb_sync(struct iommu_domain *domain,
222 struct iommu_iotlb_gather *gather)
224 struct s390_domain *s390_domain = to_s390_domain(domain);
225 size_t size = gather->end - gather->start + 1;
226 struct zpci_dev *zdev;
228 /* If gather was never added to there is nothing to flush */
233 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
234 zpci_refresh_trans((u64)zdev->fh << 32, gather->start,
240 static void s390_iommu_iotlb_sync_map(struct iommu_domain *domain,
241 unsigned long iova, size_t size)
243 struct s390_domain *s390_domain = to_s390_domain(domain);
244 struct zpci_dev *zdev;
247 list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
248 if (!zdev->tlb_refresh)
250 zpci_refresh_trans((u64)zdev->fh << 32,
256 static int s390_iommu_validate_trans(struct s390_domain *s390_domain,
257 phys_addr_t pa, dma_addr_t dma_addr,
258 unsigned long nr_pages, int flags,
261 phys_addr_t page_addr = pa & PAGE_MASK;
262 unsigned long *entry;
266 for (i = 0; i < nr_pages; i++) {
267 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr,
269 if (unlikely(!entry)) {
273 dma_update_cpu_trans(entry, page_addr, flags);
274 page_addr += PAGE_SIZE;
275 dma_addr += PAGE_SIZE;
282 dma_addr -= PAGE_SIZE;
283 entry = dma_walk_cpu_trans(s390_domain->dma_table,
287 dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
293 static int s390_iommu_invalidate_trans(struct s390_domain *s390_domain,
294 dma_addr_t dma_addr, unsigned long nr_pages)
296 unsigned long *entry;
300 for (i = 0; i < nr_pages; i++) {
301 entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr,
303 if (unlikely(!entry)) {
307 dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
308 dma_addr += PAGE_SIZE;
314 static int s390_iommu_map_pages(struct iommu_domain *domain,
315 unsigned long iova, phys_addr_t paddr,
316 size_t pgsize, size_t pgcount,
317 int prot, gfp_t gfp, size_t *mapped)
319 struct s390_domain *s390_domain = to_s390_domain(domain);
320 size_t size = pgcount << __ffs(pgsize);
321 int flags = ZPCI_PTE_VALID, rc = 0;
326 if (iova < s390_domain->domain.geometry.aperture_start ||
327 (iova + size - 1) > s390_domain->domain.geometry.aperture_end)
330 if (!IS_ALIGNED(iova | paddr, pgsize))
333 if (!(prot & IOMMU_READ))
336 if (!(prot & IOMMU_WRITE))
337 flags |= ZPCI_TABLE_PROTECTED;
339 rc = s390_iommu_validate_trans(s390_domain, paddr, iova,
340 pgcount, flags, gfp);
347 static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
350 struct s390_domain *s390_domain = to_s390_domain(domain);
351 unsigned long *rto, *sto, *pto;
352 unsigned long ste, pte, rte;
353 unsigned int rtx, sx, px;
354 phys_addr_t phys = 0;
356 if (iova < domain->geometry.aperture_start ||
357 iova > domain->geometry.aperture_end)
360 rtx = calc_rtx(iova);
363 rto = s390_domain->dma_table;
365 rte = READ_ONCE(rto[rtx]);
366 if (reg_entry_isvalid(rte)) {
367 sto = get_rt_sto(rte);
368 ste = READ_ONCE(sto[sx]);
369 if (reg_entry_isvalid(ste)) {
370 pto = get_st_pto(ste);
371 pte = READ_ONCE(pto[px]);
372 if (pt_entry_isvalid(pte))
373 phys = pte & ZPCI_PTE_ADDR_MASK;
380 static size_t s390_iommu_unmap_pages(struct iommu_domain *domain,
382 size_t pgsize, size_t pgcount,
383 struct iommu_iotlb_gather *gather)
385 struct s390_domain *s390_domain = to_s390_domain(domain);
386 size_t size = pgcount << __ffs(pgsize);
389 if (WARN_ON(iova < s390_domain->domain.geometry.aperture_start ||
390 (iova + size - 1) > s390_domain->domain.geometry.aperture_end))
393 rc = s390_iommu_invalidate_trans(s390_domain, iova, pgcount);
397 iommu_iotlb_gather_add_range(gather, iova, size);
402 int zpci_init_iommu(struct zpci_dev *zdev)
406 rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
407 "s390-iommu.%08x", zdev->fid);
411 rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
418 iommu_device_sysfs_remove(&zdev->iommu_dev);
424 void zpci_destroy_iommu(struct zpci_dev *zdev)
426 iommu_device_unregister(&zdev->iommu_dev);
427 iommu_device_sysfs_remove(&zdev->iommu_dev);
430 static const struct iommu_ops s390_iommu_ops = {
431 .capable = s390_iommu_capable,
432 .domain_alloc = s390_domain_alloc,
433 .probe_device = s390_iommu_probe_device,
434 .release_device = s390_iommu_release_device,
435 .device_group = generic_device_group,
436 .set_platform_dma_ops = s390_iommu_set_platform_dma,
437 .pgsize_bitmap = SZ_4K,
438 .get_resv_regions = s390_iommu_get_resv_regions,
439 .default_domain_ops = &(const struct iommu_domain_ops) {
440 .attach_dev = s390_iommu_attach_device,
441 .map_pages = s390_iommu_map_pages,
442 .unmap_pages = s390_iommu_unmap_pages,
443 .flush_iotlb_all = s390_iommu_flush_iotlb_all,
444 .iotlb_sync = s390_iommu_iotlb_sync,
445 .iotlb_sync_map = s390_iommu_iotlb_sync_map,
446 .iova_to_phys = s390_iommu_iova_to_phys,
447 .free = s390_domain_free,