Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Feb 10-13, 2025
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * IOMMU API for s390 PCI devices
  4 *
  5 * Copyright IBM Corp. 2015
  6 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
  7 */
  8
  9#include <linux/pci.h>
 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>
 16
 17static const struct iommu_ops s390_iommu_ops;
 
 
 
 
 
 18
 19struct s390_domain {
 20	struct iommu_domain	domain;
 21	struct list_head	devices;
 22	unsigned long		*dma_table;
 
 23	spinlock_t		list_lock;
 24	struct rcu_head		rcu;
 
 
 
 
 25};
 26
 27static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
 28{
 29	return container_of(dom, struct s390_domain, domain);
 30}
 31
 32static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap)
 33{
 34	switch (cap) {
 35	case IOMMU_CAP_CACHE_COHERENCY:
 36		return true;
 37	case IOMMU_CAP_INTR_REMAP:
 38		return true;
 39	default:
 40		return false;
 41	}
 42}
 43
 44static struct iommu_domain *s390_domain_alloc(unsigned domain_type)
 45{
 46	struct s390_domain *s390_domain;
 47
 48	if (domain_type != IOMMU_DOMAIN_UNMANAGED)
 49		return NULL;
 50
 51	s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
 52	if (!s390_domain)
 53		return NULL;
 54
 55	s390_domain->dma_table = dma_alloc_cpu_table();
 56	if (!s390_domain->dma_table) {
 57		kfree(s390_domain);
 58		return NULL;
 59	}
 60	s390_domain->domain.geometry.force_aperture = true;
 61	s390_domain->domain.geometry.aperture_start = 0;
 62	s390_domain->domain.geometry.aperture_end = ZPCI_TABLE_SIZE_RT - 1;
 63
 
 64	spin_lock_init(&s390_domain->list_lock);
 65	INIT_LIST_HEAD_RCU(&s390_domain->devices);
 66
 67	return &s390_domain->domain;
 68}
 69
 70static void s390_iommu_rcu_free_domain(struct rcu_head *head)
 71{
 72	struct s390_domain *s390_domain = container_of(head, struct s390_domain, rcu);
 73
 74	dma_cleanup_tables(s390_domain->dma_table);
 75	kfree(s390_domain);
 76}
 77
 78static void s390_domain_free(struct iommu_domain *domain)
 79{
 80	struct s390_domain *s390_domain = to_s390_domain(domain);
 81
 82	rcu_read_lock();
 83	WARN_ON(!list_empty(&s390_domain->devices));
 84	rcu_read_unlock();
 85
 86	call_rcu(&s390_domain->rcu, s390_iommu_rcu_free_domain);
 87}
 88
 89static void __s390_iommu_detach_device(struct zpci_dev *zdev)
 90{
 91	struct s390_domain *s390_domain = zdev->s390_domain;
 92	unsigned long flags;
 93
 94	if (!s390_domain)
 95		return;
 96
 97	spin_lock_irqsave(&s390_domain->list_lock, flags);
 98	list_del_rcu(&zdev->iommu_list);
 99	spin_unlock_irqrestore(&s390_domain->list_lock, flags);
100
101	zpci_unregister_ioat(zdev, 0);
102	zdev->s390_domain = NULL;
103	zdev->dma_table = NULL;
104}
105
106static int s390_iommu_attach_device(struct iommu_domain *domain,
107				    struct device *dev)
108{
109	struct s390_domain *s390_domain = to_s390_domain(domain);
110	struct zpci_dev *zdev = to_zpci_dev(dev);
 
111	unsigned long flags;
112	u8 status;
113	int cc;
114
115	if (!zdev)
116		return -ENODEV;
117
118	if (WARN_ON(domain->geometry.aperture_start > zdev->end_dma ||
119		domain->geometry.aperture_end < zdev->start_dma))
120		return -EINVAL;
121
122	if (zdev->s390_domain)
123		__s390_iommu_detach_device(zdev);
124	else if (zdev->dma_table)
125		zpci_dma_exit_device(zdev);
126
127	cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
128				virt_to_phys(s390_domain->dma_table), &status);
129	/*
130	 * If the device is undergoing error recovery the reset code
131	 * will re-establish the new domain.
132	 */
133	if (cc && status != ZPCI_PCI_ST_FUNC_NOT_AVAIL)
134		return -EIO;
135	zdev->dma_table = s390_domain->dma_table;
136
137	zdev->dma_table = s390_domain->dma_table;
138	zdev->s390_domain = s390_domain;
 
 
 
139
140	spin_lock_irqsave(&s390_domain->list_lock, flags);
141	list_add_rcu(&zdev->iommu_list, &s390_domain->devices);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142	spin_unlock_irqrestore(&s390_domain->list_lock, flags);
143
144	return 0;
 
 
 
 
 
 
145}
146
147static void s390_iommu_detach_device(struct iommu_domain *domain,
148				     struct device *dev)
149{
150	struct zpci_dev *zdev = to_zpci_dev(dev);
151
152	WARN_ON(zdev->s390_domain != to_s390_domain(domain));
153
154	__s390_iommu_detach_device(zdev);
155	zpci_dma_init_device(zdev);
156}
157
158static void s390_iommu_get_resv_regions(struct device *dev,
159					struct list_head *list)
160{
161	struct zpci_dev *zdev = to_zpci_dev(dev);
162	struct iommu_resv_region *region;
163
164	if (zdev->start_dma) {
165		region = iommu_alloc_resv_region(0, zdev->start_dma, 0,
166						 IOMMU_RESV_RESERVED, GFP_KERNEL);
167		if (!region)
168			return;
169		list_add_tail(&region->list, list);
 
 
 
170	}
 
171
172	if (zdev->end_dma < ZPCI_TABLE_SIZE_RT - 1) {
173		region = iommu_alloc_resv_region(zdev->end_dma + 1,
174						 ZPCI_TABLE_SIZE_RT - zdev->end_dma - 1,
175						 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
176		if (!region)
177			return;
178		list_add_tail(&region->list, list);
179	}
180}
181
182static struct iommu_device *s390_iommu_probe_device(struct device *dev)
183{
184	struct zpci_dev *zdev;
185
186	if (!dev_is_pci(dev))
187		return ERR_PTR(-ENODEV);
188
189	zdev = to_zpci_dev(dev);
190
191	if (zdev->start_dma > zdev->end_dma ||
192	    zdev->start_dma > ZPCI_TABLE_SIZE_RT - 1)
193		return ERR_PTR(-EINVAL);
 
 
 
194
195	if (zdev->end_dma > ZPCI_TABLE_SIZE_RT - 1)
196		zdev->end_dma = ZPCI_TABLE_SIZE_RT - 1;
197
198	return &zdev->iommu_dev;
199}
200
201static void s390_iommu_release_device(struct device *dev)
202{
203	struct zpci_dev *zdev = to_zpci_dev(dev);
 
204
205	/*
206	 * release_device is expected to detach any domain currently attached
207	 * to the device, but keep it attached to other devices in the group.
 
 
 
 
 
 
 
208	 */
209	if (zdev)
210		__s390_iommu_detach_device(zdev);
211}
212
213static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain)
214{
215	struct s390_domain *s390_domain = to_s390_domain(domain);
216	struct zpci_dev *zdev;
217
218	rcu_read_lock();
219	list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
220		zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma,
221				   zdev->end_dma - zdev->start_dma + 1);
222	}
223	rcu_read_unlock();
224}
225
226static void s390_iommu_iotlb_sync(struct iommu_domain *domain,
227				  struct iommu_iotlb_gather *gather)
228{
229	struct s390_domain *s390_domain = to_s390_domain(domain);
230	size_t size = gather->end - gather->start + 1;
231	struct zpci_dev *zdev;
232
233	/* If gather was never added to there is nothing to flush */
234	if (!gather->end)
235		return;
236
237	rcu_read_lock();
238	list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
239		zpci_refresh_trans((u64)zdev->fh << 32, gather->start,
240				   size);
241	}
242	rcu_read_unlock();
243}
244
245static void s390_iommu_iotlb_sync_map(struct iommu_domain *domain,
246				      unsigned long iova, size_t size)
 
247{
248	struct s390_domain *s390_domain = to_s390_domain(domain);
249	struct zpci_dev *zdev;
 
 
 
 
250
251	rcu_read_lock();
252	list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
253		if (!zdev->tlb_refresh)
254			continue;
255		zpci_refresh_trans((u64)zdev->fh << 32,
256				   iova, size);
257	}
258	rcu_read_unlock();
259}
260
261static int s390_iommu_validate_trans(struct s390_domain *s390_domain,
262				     phys_addr_t pa, dma_addr_t dma_addr,
263				     unsigned long nr_pages, int flags)
264{
265	phys_addr_t page_addr = pa & PAGE_MASK;
266	unsigned long *entry;
267	unsigned long i;
268	int rc;
269
 
270	for (i = 0; i < nr_pages; i++) {
271		entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
272		if (unlikely(!entry)) {
273			rc = -ENOMEM;
274			goto undo_cpu_trans;
275		}
276		dma_update_cpu_trans(entry, page_addr, flags);
277		page_addr += PAGE_SIZE;
278		dma_addr += PAGE_SIZE;
279	}
280
281	return 0;
282
283undo_cpu_trans:
284	while (i-- > 0) {
285		dma_addr -= PAGE_SIZE;
286		entry = dma_walk_cpu_trans(s390_domain->dma_table,
287					   dma_addr);
288		if (!entry)
289			break;
290		dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
291	}
 
292
293	return rc;
294}
295
296static int s390_iommu_invalidate_trans(struct s390_domain *s390_domain,
297				       dma_addr_t dma_addr, unsigned long nr_pages)
298{
299	unsigned long *entry;
300	unsigned long i;
301	int rc = 0;
302
303	for (i = 0; i < nr_pages; i++) {
304		entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
305		if (unlikely(!entry)) {
306			rc = -EINVAL;
307			break;
308		}
309		dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
310		dma_addr += PAGE_SIZE;
311	}
 
312
313	return rc;
314}
315
316static int s390_iommu_map_pages(struct iommu_domain *domain,
317				unsigned long iova, phys_addr_t paddr,
318				size_t pgsize, size_t pgcount,
319				int prot, gfp_t gfp, size_t *mapped)
320{
321	struct s390_domain *s390_domain = to_s390_domain(domain);
322	size_t size = pgcount << __ffs(pgsize);
323	int flags = ZPCI_PTE_VALID, rc = 0;
324
325	if (pgsize != SZ_4K)
326		return -EINVAL;
327
328	if (iova < s390_domain->domain.geometry.aperture_start ||
329	    (iova + size - 1) > s390_domain->domain.geometry.aperture_end)
330		return -EINVAL;
331
332	if (!IS_ALIGNED(iova | paddr, pgsize))
333		return -EINVAL;
334
335	if (!(prot & IOMMU_READ))
336		return -EINVAL;
337
338	if (!(prot & IOMMU_WRITE))
339		flags |= ZPCI_TABLE_PROTECTED;
340
341	rc = s390_iommu_validate_trans(s390_domain, paddr, iova,
342				       pgcount, flags);
343	if (!rc)
344		*mapped = size;
345
346	return rc;
347}
348
349static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
350					   dma_addr_t iova)
351{
352	struct s390_domain *s390_domain = to_s390_domain(domain);
353	unsigned long *rto, *sto, *pto;
354	unsigned long ste, pte, rte;
355	unsigned int rtx, sx, px;
356	phys_addr_t phys = 0;
357
358	if (iova < domain->geometry.aperture_start ||
359	    iova > domain->geometry.aperture_end)
360		return 0;
361
362	rtx = calc_rtx(iova);
363	sx = calc_sx(iova);
364	px = calc_px(iova);
365	rto = s390_domain->dma_table;
366
367	rte = READ_ONCE(rto[rtx]);
368	if (reg_entry_isvalid(rte)) {
369		sto = get_rt_sto(rte);
370		ste = READ_ONCE(sto[sx]);
371		if (reg_entry_isvalid(ste)) {
372			pto = get_st_pto(ste);
373			pte = READ_ONCE(pto[px]);
374			if (pt_entry_isvalid(pte))
375				phys = pte & ZPCI_PTE_ADDR_MASK;
376		}
377	}
 
378
379	return phys;
380}
381
382static size_t s390_iommu_unmap_pages(struct iommu_domain *domain,
383				     unsigned long iova,
384				     size_t pgsize, size_t pgcount,
385				     struct iommu_iotlb_gather *gather)
386{
387	struct s390_domain *s390_domain = to_s390_domain(domain);
388	size_t size = pgcount << __ffs(pgsize);
 
389	int rc;
390
391	if (WARN_ON(iova < s390_domain->domain.geometry.aperture_start ||
392	    (iova + size - 1) > s390_domain->domain.geometry.aperture_end))
393		return 0;
394
395	rc = s390_iommu_invalidate_trans(s390_domain, iova, pgcount);
 
396	if (rc)
397		return 0;
398
399	iommu_iotlb_gather_add_range(gather, iova, size);
400
401	return size;
402}
403
404int zpci_init_iommu(struct zpci_dev *zdev)
405{
406	int rc = 0;
407
408	rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
409				    "s390-iommu.%08x", zdev->fid);
410	if (rc)
411		goto out_err;
412
413	rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
414	if (rc)
415		goto out_sysfs;
416
417	return 0;
418
419out_sysfs:
420	iommu_device_sysfs_remove(&zdev->iommu_dev);
421
422out_err:
423	return rc;
424}
425
426void zpci_destroy_iommu(struct zpci_dev *zdev)
427{
428	iommu_device_unregister(&zdev->iommu_dev);
429	iommu_device_sysfs_remove(&zdev->iommu_dev);
430}
431
432static const struct iommu_ops s390_iommu_ops = {
433	.capable = s390_iommu_capable,
434	.domain_alloc = s390_domain_alloc,
435	.probe_device = s390_iommu_probe_device,
436	.release_device = s390_iommu_release_device,
437	.device_group = generic_device_group,
438	.pgsize_bitmap = SZ_4K,
439	.get_resv_regions = s390_iommu_get_resv_regions,
440	.default_domain_ops = &(const struct iommu_domain_ops) {
441		.attach_dev	= s390_iommu_attach_device,
442		.detach_dev	= s390_iommu_detach_device,
443		.map_pages	= s390_iommu_map_pages,
444		.unmap_pages	= s390_iommu_unmap_pages,
445		.flush_iotlb_all = s390_iommu_flush_iotlb_all,
446		.iotlb_sync      = s390_iommu_iotlb_sync,
447		.iotlb_sync_map  = s390_iommu_iotlb_sync_map,
448		.iova_to_phys	= s390_iommu_iova_to_phys,
449		.free		= s390_domain_free,
450	}
451};
v4.10.11
 
  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
 21struct s390_domain {
 22	struct iommu_domain	domain;
 23	struct list_head	devices;
 24	unsigned long		*dma_table;
 25	spinlock_t		dma_table_lock;
 26	spinlock_t		list_lock;
 27};
 28
 29struct s390_domain_device {
 30	struct list_head	list;
 31	struct zpci_dev		*zdev;
 32};
 33
 34static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
 35{
 36	return container_of(dom, struct s390_domain, domain);
 37}
 38
 39static bool s390_iommu_capable(enum iommu_cap cap)
 40{
 41	switch (cap) {
 42	case IOMMU_CAP_CACHE_COHERENCY:
 43		return true;
 44	case IOMMU_CAP_INTR_REMAP:
 45		return true;
 46	default:
 47		return false;
 48	}
 49}
 50
 51static struct iommu_domain *s390_domain_alloc(unsigned domain_type)
 52{
 53	struct s390_domain *s390_domain;
 54
 55	if (domain_type != IOMMU_DOMAIN_UNMANAGED)
 56		return NULL;
 57
 58	s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
 59	if (!s390_domain)
 60		return NULL;
 61
 62	s390_domain->dma_table = dma_alloc_cpu_table();
 63	if (!s390_domain->dma_table) {
 64		kfree(s390_domain);
 65		return NULL;
 66	}
 
 
 
 67
 68	spin_lock_init(&s390_domain->dma_table_lock);
 69	spin_lock_init(&s390_domain->list_lock);
 70	INIT_LIST_HEAD(&s390_domain->devices);
 71
 72	return &s390_domain->domain;
 73}
 74
 
 
 
 
 
 
 
 
 75static void s390_domain_free(struct iommu_domain *domain)
 76{
 77	struct s390_domain *s390_domain = to_s390_domain(domain);
 78
 79	dma_cleanup_tables(s390_domain->dma_table);
 80	kfree(s390_domain);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 81}
 82
 83static int s390_iommu_attach_device(struct iommu_domain *domain,
 84				    struct device *dev)
 85{
 86	struct s390_domain *s390_domain = to_s390_domain(domain);
 87	struct zpci_dev *zdev = to_pci_dev(dev)->sysdata;
 88	struct s390_domain_device *domain_device;
 89	unsigned long flags;
 90	int rc;
 
 91
 92	if (!zdev)
 93		return -ENODEV;
 94
 95	domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL);
 96	if (!domain_device)
 97		return -ENOMEM;
 98
 99	if (zdev->dma_table)
 
 
100		zpci_dma_exit_device(zdev);
101
 
 
 
 
 
 
 
 
 
 
102	zdev->dma_table = s390_domain->dma_table;
103	rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
104				(u64) zdev->dma_table);
105	if (rc)
106		goto out_restore;
107
108	spin_lock_irqsave(&s390_domain->list_lock, flags);
109	/* First device defines the DMA range limits */
110	if (list_empty(&s390_domain->devices)) {
111		domain->geometry.aperture_start = zdev->start_dma;
112		domain->geometry.aperture_end = zdev->end_dma;
113		domain->geometry.force_aperture = true;
114	/* Allow only devices with identical DMA range limits */
115	} else if (domain->geometry.aperture_start != zdev->start_dma ||
116		   domain->geometry.aperture_end != zdev->end_dma) {
117		rc = -EINVAL;
118		spin_unlock_irqrestore(&s390_domain->list_lock, flags);
119		goto out_restore;
120	}
121	domain_device->zdev = zdev;
122	zdev->s390_domain = s390_domain;
123	list_add(&domain_device->list, &s390_domain->devices);
124	spin_unlock_irqrestore(&s390_domain->list_lock, flags);
125
126	return 0;
127
128out_restore:
129	zpci_dma_init_device(zdev);
130	kfree(domain_device);
131
132	return rc;
133}
134
135static void s390_iommu_detach_device(struct iommu_domain *domain,
136				     struct device *dev)
137{
138	struct s390_domain *s390_domain = to_s390_domain(domain);
139	struct zpci_dev *zdev = to_pci_dev(dev)->sysdata;
140	struct s390_domain_device *domain_device, *tmp;
141	unsigned long flags;
142	int found = 0;
 
 
143
144	if (!zdev)
145		return;
 
 
 
146
147	spin_lock_irqsave(&s390_domain->list_lock, flags);
148	list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices,
149				 list) {
150		if (domain_device->zdev == zdev) {
151			list_del(&domain_device->list);
152			kfree(domain_device);
153			found = 1;
154			break;
155		}
156	}
157	spin_unlock_irqrestore(&s390_domain->list_lock, flags);
158
159	if (found) {
160		zdev->s390_domain = NULL;
161		zpci_unregister_ioat(zdev, 0);
162		zpci_dma_init_device(zdev);
 
 
 
163	}
164}
165
166static int s390_iommu_add_device(struct device *dev)
167{
168	struct iommu_group *group;
169	int rc;
 
 
 
 
170
171	group = iommu_group_get(dev);
172	if (!group) {
173		group = iommu_group_alloc();
174		if (IS_ERR(group))
175			return PTR_ERR(group);
176	}
177
178	rc = iommu_group_add_device(group, dev);
179	iommu_group_put(group);
180
181	return rc;
182}
183
184static void s390_iommu_remove_device(struct device *dev)
185{
186	struct zpci_dev *zdev = to_pci_dev(dev)->sysdata;
187	struct iommu_domain *domain;
188
189	/*
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 remove_device will be called via the BUS_NOTIFY_REMOVED_DEVICE
196	 * notifier.
197	 *
198	 * So let's call detach_dev from here if it hasn't been called before.
199	 */
200	if (zdev && zdev->s390_domain) {
201		domain = iommu_get_domain_for_dev(dev);
202		if (domain)
203			s390_iommu_detach_device(domain, dev);
 
 
 
 
 
 
 
 
 
204	}
 
 
 
 
 
 
 
 
 
 
 
 
 
205
206	iommu_group_remove_device(dev);
 
 
 
 
 
207}
208
209static int s390_iommu_update_trans(struct s390_domain *s390_domain,
210				   unsigned long pa, dma_addr_t dma_addr,
211				   size_t size, int flags)
212{
213	struct s390_domain_device *domain_device;
214	u8 *page_addr = (u8 *) (pa & PAGE_MASK);
215	dma_addr_t start_dma_addr = dma_addr;
216	unsigned long irq_flags, nr_pages, i;
217	unsigned long *entry;
218	int rc = 0;
219
220	if (dma_addr < s390_domain->domain.geometry.aperture_start ||
221	    dma_addr + size > s390_domain->domain.geometry.aperture_end)
222		return -EINVAL;
 
 
 
 
 
 
223
224	nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
225	if (!nr_pages)
226		return 0;
 
 
 
 
 
227
228	spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags);
229	for (i = 0; i < nr_pages; i++) {
230		entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
231		if (!entry) {
232			rc = -ENOMEM;
233			goto undo_cpu_trans;
234		}
235		dma_update_cpu_trans(entry, page_addr, flags);
236		page_addr += PAGE_SIZE;
237		dma_addr += PAGE_SIZE;
238	}
239
240	spin_lock(&s390_domain->list_lock);
241	list_for_each_entry(domain_device, &s390_domain->devices, list) {
242		rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32,
243					start_dma_addr, nr_pages * PAGE_SIZE);
244		if (rc)
 
 
 
245			break;
 
246	}
247	spin_unlock(&s390_domain->list_lock);
248
249undo_cpu_trans:
250	if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
251		flags = ZPCI_PTE_INVALID;
252		while (i-- > 0) {
253			page_addr -= PAGE_SIZE;
254			dma_addr -= PAGE_SIZE;
255			entry = dma_walk_cpu_trans(s390_domain->dma_table,
256						   dma_addr);
257			if (!entry)
258				break;
259			dma_update_cpu_trans(entry, page_addr, flags);
 
 
 
 
260		}
 
 
261	}
262	spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags);
263
264	return rc;
265}
266
267static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
268			  phys_addr_t paddr, size_t size, int prot)
 
 
269{
270	struct s390_domain *s390_domain = to_s390_domain(domain);
 
271	int flags = ZPCI_PTE_VALID, rc = 0;
272
 
 
 
 
 
 
 
 
 
 
273	if (!(prot & IOMMU_READ))
274		return -EINVAL;
275
276	if (!(prot & IOMMU_WRITE))
277		flags |= ZPCI_TABLE_PROTECTED;
278
279	rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
280				     size, flags);
 
 
281
282	return rc;
283}
284
285static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
286					   dma_addr_t iova)
287{
288	struct s390_domain *s390_domain = to_s390_domain(domain);
289	unsigned long *sto, *pto, *rto, flags;
 
290	unsigned int rtx, sx, px;
291	phys_addr_t phys = 0;
292
293	if (iova < domain->geometry.aperture_start ||
294	    iova > domain->geometry.aperture_end)
295		return 0;
296
297	rtx = calc_rtx(iova);
298	sx = calc_sx(iova);
299	px = calc_px(iova);
300	rto = s390_domain->dma_table;
301
302	spin_lock_irqsave(&s390_domain->dma_table_lock, flags);
303	if (rto && reg_entry_isvalid(rto[rtx])) {
304		sto = get_rt_sto(rto[rtx]);
305		if (sto && reg_entry_isvalid(sto[sx])) {
306			pto = get_st_pto(sto[sx]);
307			if (pto && pt_entry_isvalid(pto[px]))
308				phys = pto[px] & ZPCI_PTE_ADDR_MASK;
 
 
309		}
310	}
311	spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags);
312
313	return phys;
314}
315
316static size_t s390_iommu_unmap(struct iommu_domain *domain,
317			       unsigned long iova, size_t size)
 
 
318{
319	struct s390_domain *s390_domain = to_s390_domain(domain);
320	int flags = ZPCI_PTE_INVALID;
321	phys_addr_t paddr;
322	int rc;
323
324	paddr = s390_iommu_iova_to_phys(domain, iova);
325	if (!paddr)
326		return 0;
327
328	rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
329				     size, flags);
330	if (rc)
331		return 0;
332
 
 
333	return size;
334}
335
336static struct iommu_ops s390_iommu_ops = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337	.capable = s390_iommu_capable,
338	.domain_alloc = s390_domain_alloc,
339	.domain_free = s390_domain_free,
340	.attach_dev = s390_iommu_attach_device,
341	.detach_dev = s390_iommu_detach_device,
342	.map = s390_iommu_map,
343	.unmap = s390_iommu_unmap,
344	.iova_to_phys = s390_iommu_iova_to_phys,
345	.add_device = s390_iommu_add_device,
346	.remove_device = s390_iommu_remove_device,
347	.pgsize_bitmap = S390_IOMMU_PGSIZES,
 
 
 
 
 
 
 
348};
349
350static int __init s390_iommu_init(void)
351{
352	return bus_set_iommu(&pci_bus_type, &s390_iommu_ops);
353}
354subsys_initcall(s390_iommu_init);