xref: /linux/drivers/iommu/s390-iommu.c (revision 0cc6f45cecb46cefe89c17ec816dc8cd58a2229a)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28128f23cSGerald Schaefer /*
38128f23cSGerald Schaefer  * IOMMU API for s390 PCI devices
48128f23cSGerald Schaefer  *
58128f23cSGerald Schaefer  * Copyright IBM Corp. 2015
68128f23cSGerald Schaefer  * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
78128f23cSGerald Schaefer  */
88128f23cSGerald Schaefer 
98128f23cSGerald Schaefer #include <linux/pci.h>
108128f23cSGerald Schaefer #include <linux/iommu.h>
118128f23cSGerald Schaefer #include <linux/iommu-helper.h>
128128f23cSGerald Schaefer #include <linux/sizes.h>
132ba8336dSNiklas Schnelle #include <linux/rculist.h>
142ba8336dSNiklas Schnelle #include <linux/rcupdate.h>
158128f23cSGerald Schaefer #include <asm/pci_dma.h>
168128f23cSGerald Schaefer 
17c76c067eSNiklas Schnelle #include "dma-iommu.h"
18c76c067eSNiklas Schnelle 
19cceb8451SArvind Yadav static const struct iommu_ops s390_iommu_ops;
20f42c2235SJoerg Roedel 
21c76c067eSNiklas Schnelle static struct kmem_cache *dma_region_table_cache;
22c76c067eSNiklas Schnelle static struct kmem_cache *dma_page_table_cache;
23c76c067eSNiklas Schnelle 
24c76c067eSNiklas Schnelle static u64 s390_iommu_aperture;
25c76c067eSNiklas Schnelle static u32 s390_iommu_aperture_factor = 1;
26c76c067eSNiklas Schnelle 
278128f23cSGerald Schaefer struct s390_domain {
288128f23cSGerald Schaefer 	struct iommu_domain	domain;
298128f23cSGerald Schaefer 	struct list_head	devices;
30c76c067eSNiklas Schnelle 	struct zpci_iommu_ctrs	ctrs;
318128f23cSGerald Schaefer 	unsigned long		*dma_table;
328128f23cSGerald Schaefer 	spinlock_t		list_lock;
332ba8336dSNiklas Schnelle 	struct rcu_head		rcu;
348128f23cSGerald Schaefer };
358128f23cSGerald Schaefer 
calc_rtx(dma_addr_t ptr)36c76c067eSNiklas Schnelle static inline unsigned int calc_rtx(dma_addr_t ptr)
37c76c067eSNiklas Schnelle {
38c76c067eSNiklas Schnelle 	return ((unsigned long)ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
39c76c067eSNiklas Schnelle }
40c76c067eSNiklas Schnelle 
calc_sx(dma_addr_t ptr)41c76c067eSNiklas Schnelle static inline unsigned int calc_sx(dma_addr_t ptr)
42c76c067eSNiklas Schnelle {
43c76c067eSNiklas Schnelle 	return ((unsigned long)ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
44c76c067eSNiklas Schnelle }
45c76c067eSNiklas Schnelle 
calc_px(dma_addr_t ptr)46c76c067eSNiklas Schnelle static inline unsigned int calc_px(dma_addr_t ptr)
47c76c067eSNiklas Schnelle {
48c76c067eSNiklas Schnelle 	return ((unsigned long)ptr >> PAGE_SHIFT) & ZPCI_PT_MASK;
49c76c067eSNiklas Schnelle }
50c76c067eSNiklas Schnelle 
set_pt_pfaa(unsigned long * entry,phys_addr_t pfaa)51c76c067eSNiklas Schnelle static inline void set_pt_pfaa(unsigned long *entry, phys_addr_t pfaa)
52c76c067eSNiklas Schnelle {
53c76c067eSNiklas Schnelle 	*entry &= ZPCI_PTE_FLAG_MASK;
54c76c067eSNiklas Schnelle 	*entry |= (pfaa & ZPCI_PTE_ADDR_MASK);
55c76c067eSNiklas Schnelle }
56c76c067eSNiklas Schnelle 
set_rt_sto(unsigned long * entry,phys_addr_t sto)57c76c067eSNiklas Schnelle static inline void set_rt_sto(unsigned long *entry, phys_addr_t sto)
58c76c067eSNiklas Schnelle {
59c76c067eSNiklas Schnelle 	*entry &= ZPCI_RTE_FLAG_MASK;
60c76c067eSNiklas Schnelle 	*entry |= (sto & ZPCI_RTE_ADDR_MASK);
61c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_TYPE_RTX;
62c76c067eSNiklas Schnelle }
63c76c067eSNiklas Schnelle 
set_st_pto(unsigned long * entry,phys_addr_t pto)64c76c067eSNiklas Schnelle static inline void set_st_pto(unsigned long *entry, phys_addr_t pto)
65c76c067eSNiklas Schnelle {
66c76c067eSNiklas Schnelle 	*entry &= ZPCI_STE_FLAG_MASK;
67c76c067eSNiklas Schnelle 	*entry |= (pto & ZPCI_STE_ADDR_MASK);
68c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_TYPE_SX;
69c76c067eSNiklas Schnelle }
70c76c067eSNiklas Schnelle 
validate_rt_entry(unsigned long * entry)71c76c067eSNiklas Schnelle static inline void validate_rt_entry(unsigned long *entry)
72c76c067eSNiklas Schnelle {
73c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_TABLE_VALID_MASK;
74c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_TABLE_OFFSET_MASK;
75c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_VALID;
76c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_LEN_RTX;
77c76c067eSNiklas Schnelle }
78c76c067eSNiklas Schnelle 
validate_st_entry(unsigned long * entry)79c76c067eSNiklas Schnelle static inline void validate_st_entry(unsigned long *entry)
80c76c067eSNiklas Schnelle {
81c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_TABLE_VALID_MASK;
82c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_VALID;
83c76c067eSNiklas Schnelle }
84c76c067eSNiklas Schnelle 
invalidate_pt_entry(unsigned long * entry)85c76c067eSNiklas Schnelle static inline void invalidate_pt_entry(unsigned long *entry)
86c76c067eSNiklas Schnelle {
87c76c067eSNiklas Schnelle 	WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_INVALID);
88c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_PTE_VALID_MASK;
89c76c067eSNiklas Schnelle 	*entry |= ZPCI_PTE_INVALID;
90c76c067eSNiklas Schnelle }
91c76c067eSNiklas Schnelle 
validate_pt_entry(unsigned long * entry)92c76c067eSNiklas Schnelle static inline void validate_pt_entry(unsigned long *entry)
93c76c067eSNiklas Schnelle {
94c76c067eSNiklas Schnelle 	WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID);
95c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_PTE_VALID_MASK;
96c76c067eSNiklas Schnelle 	*entry |= ZPCI_PTE_VALID;
97c76c067eSNiklas Schnelle }
98c76c067eSNiklas Schnelle 
entry_set_protected(unsigned long * entry)99c76c067eSNiklas Schnelle static inline void entry_set_protected(unsigned long *entry)
100c76c067eSNiklas Schnelle {
101c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_TABLE_PROT_MASK;
102c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_PROTECTED;
103c76c067eSNiklas Schnelle }
104c76c067eSNiklas Schnelle 
entry_clr_protected(unsigned long * entry)105c76c067eSNiklas Schnelle static inline void entry_clr_protected(unsigned long *entry)
106c76c067eSNiklas Schnelle {
107c76c067eSNiklas Schnelle 	*entry &= ~ZPCI_TABLE_PROT_MASK;
108c76c067eSNiklas Schnelle 	*entry |= ZPCI_TABLE_UNPROTECTED;
109c76c067eSNiklas Schnelle }
110c76c067eSNiklas Schnelle 
reg_entry_isvalid(unsigned long entry)111c76c067eSNiklas Schnelle static inline int reg_entry_isvalid(unsigned long entry)
112c76c067eSNiklas Schnelle {
113c76c067eSNiklas Schnelle 	return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID;
114c76c067eSNiklas Schnelle }
115c76c067eSNiklas Schnelle 
pt_entry_isvalid(unsigned long entry)116c76c067eSNiklas Schnelle static inline int pt_entry_isvalid(unsigned long entry)
117c76c067eSNiklas Schnelle {
118c76c067eSNiklas Schnelle 	return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID;
119c76c067eSNiklas Schnelle }
120c76c067eSNiklas Schnelle 
get_rt_sto(unsigned long entry)121c76c067eSNiklas Schnelle static inline unsigned long *get_rt_sto(unsigned long entry)
122c76c067eSNiklas Schnelle {
123c76c067eSNiklas Schnelle 	if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
124c76c067eSNiklas Schnelle 		return phys_to_virt(entry & ZPCI_RTE_ADDR_MASK);
125c76c067eSNiklas Schnelle 	else
126c76c067eSNiklas Schnelle 		return NULL;
127c76c067eSNiklas Schnelle }
128c76c067eSNiklas Schnelle 
get_st_pto(unsigned long entry)129c76c067eSNiklas Schnelle static inline unsigned long *get_st_pto(unsigned long entry)
130c76c067eSNiklas Schnelle {
131c76c067eSNiklas Schnelle 	if ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
132c76c067eSNiklas Schnelle 		return phys_to_virt(entry & ZPCI_STE_ADDR_MASK);
133c76c067eSNiklas Schnelle 	else
134c76c067eSNiklas Schnelle 		return NULL;
135c76c067eSNiklas Schnelle }
136c76c067eSNiklas Schnelle 
dma_alloc_cpu_table_caches(void)137c76c067eSNiklas Schnelle static int __init dma_alloc_cpu_table_caches(void)
138c76c067eSNiklas Schnelle {
139c76c067eSNiklas Schnelle 	dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
140c76c067eSNiklas Schnelle 						   ZPCI_TABLE_SIZE,
141c76c067eSNiklas Schnelle 						   ZPCI_TABLE_ALIGN,
142c76c067eSNiklas Schnelle 						   0, NULL);
143c76c067eSNiklas Schnelle 	if (!dma_region_table_cache)
144c76c067eSNiklas Schnelle 		return -ENOMEM;
145c76c067eSNiklas Schnelle 
146c76c067eSNiklas Schnelle 	dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
147c76c067eSNiklas Schnelle 						 ZPCI_PT_SIZE,
148c76c067eSNiklas Schnelle 						 ZPCI_PT_ALIGN,
149c76c067eSNiklas Schnelle 						 0, NULL);
150c76c067eSNiklas Schnelle 	if (!dma_page_table_cache) {
151c76c067eSNiklas Schnelle 		kmem_cache_destroy(dma_region_table_cache);
152c76c067eSNiklas Schnelle 		return -ENOMEM;
153c76c067eSNiklas Schnelle 	}
154c76c067eSNiklas Schnelle 	return 0;
155c76c067eSNiklas Schnelle }
156c76c067eSNiklas Schnelle 
dma_alloc_cpu_table(gfp_t gfp)157c76c067eSNiklas Schnelle static unsigned long *dma_alloc_cpu_table(gfp_t gfp)
158c76c067eSNiklas Schnelle {
159c76c067eSNiklas Schnelle 	unsigned long *table, *entry;
160c76c067eSNiklas Schnelle 
161c76c067eSNiklas Schnelle 	table = kmem_cache_alloc(dma_region_table_cache, gfp);
162c76c067eSNiklas Schnelle 	if (!table)
163c76c067eSNiklas Schnelle 		return NULL;
164c76c067eSNiklas Schnelle 
165c76c067eSNiklas Schnelle 	for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
166c76c067eSNiklas Schnelle 		*entry = ZPCI_TABLE_INVALID;
167c76c067eSNiklas Schnelle 	return table;
168c76c067eSNiklas Schnelle }
169c76c067eSNiklas Schnelle 
dma_free_cpu_table(void * table)170c76c067eSNiklas Schnelle static void dma_free_cpu_table(void *table)
171c76c067eSNiklas Schnelle {
172c76c067eSNiklas Schnelle 	kmem_cache_free(dma_region_table_cache, table);
173c76c067eSNiklas Schnelle }
174c76c067eSNiklas Schnelle 
dma_free_page_table(void * table)175c76c067eSNiklas Schnelle static void dma_free_page_table(void *table)
176c76c067eSNiklas Schnelle {
177c76c067eSNiklas Schnelle 	kmem_cache_free(dma_page_table_cache, table);
178c76c067eSNiklas Schnelle }
179c76c067eSNiklas Schnelle 
dma_free_seg_table(unsigned long entry)180c76c067eSNiklas Schnelle static void dma_free_seg_table(unsigned long entry)
181c76c067eSNiklas Schnelle {
182c76c067eSNiklas Schnelle 	unsigned long *sto = get_rt_sto(entry);
183c76c067eSNiklas Schnelle 	int sx;
184c76c067eSNiklas Schnelle 
185c76c067eSNiklas Schnelle 	for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
186c76c067eSNiklas Schnelle 		if (reg_entry_isvalid(sto[sx]))
187c76c067eSNiklas Schnelle 			dma_free_page_table(get_st_pto(sto[sx]));
188c76c067eSNiklas Schnelle 
189c76c067eSNiklas Schnelle 	dma_free_cpu_table(sto);
190c76c067eSNiklas Schnelle }
191c76c067eSNiklas Schnelle 
dma_cleanup_tables(unsigned long * table)192c76c067eSNiklas Schnelle static void dma_cleanup_tables(unsigned long *table)
193c76c067eSNiklas Schnelle {
194c76c067eSNiklas Schnelle 	int rtx;
195c76c067eSNiklas Schnelle 
196c76c067eSNiklas Schnelle 	if (!table)
197c76c067eSNiklas Schnelle 		return;
198c76c067eSNiklas Schnelle 
199c76c067eSNiklas Schnelle 	for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
200c76c067eSNiklas Schnelle 		if (reg_entry_isvalid(table[rtx]))
201c76c067eSNiklas Schnelle 			dma_free_seg_table(table[rtx]);
202c76c067eSNiklas Schnelle 
203c76c067eSNiklas Schnelle 	dma_free_cpu_table(table);
204c76c067eSNiklas Schnelle }
205c76c067eSNiklas Schnelle 
dma_alloc_page_table(gfp_t gfp)206c76c067eSNiklas Schnelle static unsigned long *dma_alloc_page_table(gfp_t gfp)
207c76c067eSNiklas Schnelle {
208c76c067eSNiklas Schnelle 	unsigned long *table, *entry;
209c76c067eSNiklas Schnelle 
210c76c067eSNiklas Schnelle 	table = kmem_cache_alloc(dma_page_table_cache, gfp);
211c76c067eSNiklas Schnelle 	if (!table)
212c76c067eSNiklas Schnelle 		return NULL;
213c76c067eSNiklas Schnelle 
214c76c067eSNiklas Schnelle 	for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
215c76c067eSNiklas Schnelle 		*entry = ZPCI_PTE_INVALID;
216c76c067eSNiklas Schnelle 	return table;
217c76c067eSNiklas Schnelle }
218c76c067eSNiklas Schnelle 
dma_get_seg_table_origin(unsigned long * rtep,gfp_t gfp)219c76c067eSNiklas Schnelle static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp)
220c76c067eSNiklas Schnelle {
221c76c067eSNiklas Schnelle 	unsigned long old_rte, rte;
222c76c067eSNiklas Schnelle 	unsigned long *sto;
223c76c067eSNiklas Schnelle 
224c76c067eSNiklas Schnelle 	rte = READ_ONCE(*rtep);
225c76c067eSNiklas Schnelle 	if (reg_entry_isvalid(rte)) {
226c76c067eSNiklas Schnelle 		sto = get_rt_sto(rte);
227c76c067eSNiklas Schnelle 	} else {
228c76c067eSNiklas Schnelle 		sto = dma_alloc_cpu_table(gfp);
229c76c067eSNiklas Schnelle 		if (!sto)
230c76c067eSNiklas Schnelle 			return NULL;
231c76c067eSNiklas Schnelle 
232c76c067eSNiklas Schnelle 		set_rt_sto(&rte, virt_to_phys(sto));
233c76c067eSNiklas Schnelle 		validate_rt_entry(&rte);
234c76c067eSNiklas Schnelle 		entry_clr_protected(&rte);
235c76c067eSNiklas Schnelle 
236c76c067eSNiklas Schnelle 		old_rte = cmpxchg(rtep, ZPCI_TABLE_INVALID, rte);
237c76c067eSNiklas Schnelle 		if (old_rte != ZPCI_TABLE_INVALID) {
238c76c067eSNiklas Schnelle 			/* Somone else was faster, use theirs */
239c76c067eSNiklas Schnelle 			dma_free_cpu_table(sto);
240c76c067eSNiklas Schnelle 			sto = get_rt_sto(old_rte);
241c76c067eSNiklas Schnelle 		}
242c76c067eSNiklas Schnelle 	}
243c76c067eSNiklas Schnelle 	return sto;
244c76c067eSNiklas Schnelle }
245c76c067eSNiklas Schnelle 
dma_get_page_table_origin(unsigned long * step,gfp_t gfp)246c76c067eSNiklas Schnelle static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp)
247c76c067eSNiklas Schnelle {
248c76c067eSNiklas Schnelle 	unsigned long old_ste, ste;
249c76c067eSNiklas Schnelle 	unsigned long *pto;
250c76c067eSNiklas Schnelle 
251c76c067eSNiklas Schnelle 	ste = READ_ONCE(*step);
252c76c067eSNiklas Schnelle 	if (reg_entry_isvalid(ste)) {
253c76c067eSNiklas Schnelle 		pto = get_st_pto(ste);
254c76c067eSNiklas Schnelle 	} else {
255c76c067eSNiklas Schnelle 		pto = dma_alloc_page_table(gfp);
256c76c067eSNiklas Schnelle 		if (!pto)
257c76c067eSNiklas Schnelle 			return NULL;
258c76c067eSNiklas Schnelle 		set_st_pto(&ste, virt_to_phys(pto));
259c76c067eSNiklas Schnelle 		validate_st_entry(&ste);
260c76c067eSNiklas Schnelle 		entry_clr_protected(&ste);
261c76c067eSNiklas Schnelle 
262c76c067eSNiklas Schnelle 		old_ste = cmpxchg(step, ZPCI_TABLE_INVALID, ste);
263c76c067eSNiklas Schnelle 		if (old_ste != ZPCI_TABLE_INVALID) {
264c76c067eSNiklas Schnelle 			/* Somone else was faster, use theirs */
265c76c067eSNiklas Schnelle 			dma_free_page_table(pto);
266c76c067eSNiklas Schnelle 			pto = get_st_pto(old_ste);
267c76c067eSNiklas Schnelle 		}
268c76c067eSNiklas Schnelle 	}
269c76c067eSNiklas Schnelle 	return pto;
270c76c067eSNiklas Schnelle }
271c76c067eSNiklas Schnelle 
dma_walk_cpu_trans(unsigned long * rto,dma_addr_t dma_addr,gfp_t gfp)272c76c067eSNiklas Schnelle static unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr, gfp_t gfp)
273c76c067eSNiklas Schnelle {
274c76c067eSNiklas Schnelle 	unsigned long *sto, *pto;
275c76c067eSNiklas Schnelle 	unsigned int rtx, sx, px;
276c76c067eSNiklas Schnelle 
277c76c067eSNiklas Schnelle 	rtx = calc_rtx(dma_addr);
278c76c067eSNiklas Schnelle 	sto = dma_get_seg_table_origin(&rto[rtx], gfp);
279c76c067eSNiklas Schnelle 	if (!sto)
280c76c067eSNiklas Schnelle 		return NULL;
281c76c067eSNiklas Schnelle 
282c76c067eSNiklas Schnelle 	sx = calc_sx(dma_addr);
283c76c067eSNiklas Schnelle 	pto = dma_get_page_table_origin(&sto[sx], gfp);
284c76c067eSNiklas Schnelle 	if (!pto)
285c76c067eSNiklas Schnelle 		return NULL;
286c76c067eSNiklas Schnelle 
287c76c067eSNiklas Schnelle 	px = calc_px(dma_addr);
288c76c067eSNiklas Schnelle 	return &pto[px];
289c76c067eSNiklas Schnelle }
290c76c067eSNiklas Schnelle 
dma_update_cpu_trans(unsigned long * ptep,phys_addr_t page_addr,int flags)291c76c067eSNiklas Schnelle static void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags)
292c76c067eSNiklas Schnelle {
293c76c067eSNiklas Schnelle 	unsigned long pte;
294c76c067eSNiklas Schnelle 
295c76c067eSNiklas Schnelle 	pte = READ_ONCE(*ptep);
296c76c067eSNiklas Schnelle 	if (flags & ZPCI_PTE_INVALID) {
297c76c067eSNiklas Schnelle 		invalidate_pt_entry(&pte);
298c76c067eSNiklas Schnelle 	} else {
299c76c067eSNiklas Schnelle 		set_pt_pfaa(&pte, page_addr);
300c76c067eSNiklas Schnelle 		validate_pt_entry(&pte);
301c76c067eSNiklas Schnelle 	}
302c76c067eSNiklas Schnelle 
303c76c067eSNiklas Schnelle 	if (flags & ZPCI_TABLE_PROTECTED)
304c76c067eSNiklas Schnelle 		entry_set_protected(&pte);
305c76c067eSNiklas Schnelle 	else
306c76c067eSNiklas Schnelle 		entry_clr_protected(&pte);
307c76c067eSNiklas Schnelle 
308c76c067eSNiklas Schnelle 	xchg(ptep, pte);
309c76c067eSNiklas Schnelle }
310c76c067eSNiklas Schnelle 
to_s390_domain(struct iommu_domain * dom)3118128f23cSGerald Schaefer static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
3128128f23cSGerald Schaefer {
3138128f23cSGerald Schaefer 	return container_of(dom, struct s390_domain, domain);
3148128f23cSGerald Schaefer }
3158128f23cSGerald Schaefer 
s390_iommu_capable(struct device * dev,enum iommu_cap cap)316359ad157SRobin Murphy static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap)
3178128f23cSGerald Schaefer {
31853f8e9adSNiklas Schnelle 	struct zpci_dev *zdev = to_zpci_dev(dev);
31953f8e9adSNiklas Schnelle 
3208128f23cSGerald Schaefer 	switch (cap) {
3218128f23cSGerald Schaefer 	case IOMMU_CAP_CACHE_COHERENCY:
3228128f23cSGerald Schaefer 		return true;
323c76c067eSNiklas Schnelle 	case IOMMU_CAP_DEFERRED_FLUSH:
32453f8e9adSNiklas Schnelle 		return zdev->pft != PCI_FUNC_TYPE_ISM;
3258128f23cSGerald Schaefer 	default:
3268128f23cSGerald Schaefer 		return false;
3278128f23cSGerald Schaefer 	}
3288128f23cSGerald Schaefer }
3298128f23cSGerald Schaefer 
s390_domain_alloc_paging(struct device * dev)3304efd98d4SJason Gunthorpe static struct iommu_domain *s390_domain_alloc_paging(struct device *dev)
3318128f23cSGerald Schaefer {
3328128f23cSGerald Schaefer 	struct s390_domain *s390_domain;
3338128f23cSGerald Schaefer 
3348128f23cSGerald Schaefer 	s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
3358128f23cSGerald Schaefer 	if (!s390_domain)
3368128f23cSGerald Schaefer 		return NULL;
3378128f23cSGerald Schaefer 
338429f27e3SJason Gunthorpe 	s390_domain->dma_table = dma_alloc_cpu_table(GFP_KERNEL);
3398128f23cSGerald Schaefer 	if (!s390_domain->dma_table) {
3408128f23cSGerald Schaefer 		kfree(s390_domain);
3418128f23cSGerald Schaefer 		return NULL;
3428128f23cSGerald Schaefer 	}
343cbf7827bSNiklas Schnelle 	s390_domain->domain.geometry.force_aperture = true;
344cbf7827bSNiklas Schnelle 	s390_domain->domain.geometry.aperture_start = 0;
345cbf7827bSNiklas Schnelle 	s390_domain->domain.geometry.aperture_end = ZPCI_TABLE_SIZE_RT - 1;
3468128f23cSGerald Schaefer 
3478128f23cSGerald Schaefer 	spin_lock_init(&s390_domain->list_lock);
3482ba8336dSNiklas Schnelle 	INIT_LIST_HEAD_RCU(&s390_domain->devices);
3498128f23cSGerald Schaefer 
3508128f23cSGerald Schaefer 	return &s390_domain->domain;
3518128f23cSGerald Schaefer }
3528128f23cSGerald Schaefer 
s390_iommu_rcu_free_domain(struct rcu_head * head)3532ba8336dSNiklas Schnelle static void s390_iommu_rcu_free_domain(struct rcu_head *head)
3542ba8336dSNiklas Schnelle {
3552ba8336dSNiklas Schnelle 	struct s390_domain *s390_domain = container_of(head, struct s390_domain, rcu);
3562ba8336dSNiklas Schnelle 
3572ba8336dSNiklas Schnelle 	dma_cleanup_tables(s390_domain->dma_table);
3582ba8336dSNiklas Schnelle 	kfree(s390_domain);
3592ba8336dSNiklas Schnelle }
3602ba8336dSNiklas Schnelle 
s390_domain_free(struct iommu_domain * domain)3617cd75787SSebastian Ott static void s390_domain_free(struct iommu_domain *domain)
3628128f23cSGerald Schaefer {
3638128f23cSGerald Schaefer 	struct s390_domain *s390_domain = to_s390_domain(domain);
3648128f23cSGerald Schaefer 
3652ba8336dSNiklas Schnelle 	rcu_read_lock();
366bf8d2dd2SNiklas Schnelle 	WARN_ON(!list_empty(&s390_domain->devices));
3672ba8336dSNiklas Schnelle 	rcu_read_unlock();
3682ba8336dSNiklas Schnelle 
3692ba8336dSNiklas Schnelle 	call_rcu(&s390_domain->rcu, s390_iommu_rcu_free_domain);
3708128f23cSGerald Schaefer }
3718128f23cSGerald Schaefer 
s390_iommu_detach_device(struct iommu_domain * domain,struct device * dev)372c76c067eSNiklas Schnelle static void s390_iommu_detach_device(struct iommu_domain *domain,
373c76c067eSNiklas Schnelle 				     struct device *dev)
374bf8d2dd2SNiklas Schnelle {
375c76c067eSNiklas Schnelle 	struct s390_domain *s390_domain = to_s390_domain(domain);
376c76c067eSNiklas Schnelle 	struct zpci_dev *zdev = to_zpci_dev(dev);
377bf8d2dd2SNiklas Schnelle 	unsigned long flags;
378bf8d2dd2SNiklas Schnelle 
379bf8d2dd2SNiklas Schnelle 	spin_lock_irqsave(&s390_domain->list_lock, flags);
3802ba8336dSNiklas Schnelle 	list_del_rcu(&zdev->iommu_list);
381bf8d2dd2SNiklas Schnelle 	spin_unlock_irqrestore(&s390_domain->list_lock, flags);
382bf8d2dd2SNiklas Schnelle 
383bf8d2dd2SNiklas Schnelle 	zpci_unregister_ioat(zdev, 0);
384bf8d2dd2SNiklas Schnelle 	zdev->s390_domain = NULL;
385bf8d2dd2SNiklas Schnelle 	zdev->dma_table = NULL;
386bf8d2dd2SNiklas Schnelle }
387bf8d2dd2SNiklas Schnelle 
s390_iommu_attach_device(struct iommu_domain * domain,struct device * dev)3888128f23cSGerald Schaefer static int s390_iommu_attach_device(struct iommu_domain *domain,
3898128f23cSGerald Schaefer 				    struct device *dev)
3908128f23cSGerald Schaefer {
3918128f23cSGerald Schaefer 	struct s390_domain *s390_domain = to_s390_domain(domain);
392d08d6f5dSPierre Morel 	struct zpci_dev *zdev = to_zpci_dev(dev);
3938128f23cSGerald Schaefer 	unsigned long flags;
39459bbf596SNiklas Schnelle 	u8 status;
395cbf7827bSNiklas Schnelle 	int cc;
3968128f23cSGerald Schaefer 
3978128f23cSGerald Schaefer 	if (!zdev)
3988128f23cSGerald Schaefer 		return -ENODEV;
3998128f23cSGerald Schaefer 
400cbf7827bSNiklas Schnelle 	if (WARN_ON(domain->geometry.aperture_start > zdev->end_dma ||
401cbf7827bSNiklas Schnelle 		domain->geometry.aperture_end < zdev->start_dma))
402cbf7827bSNiklas Schnelle 		return -EINVAL;
403cbf7827bSNiklas Schnelle 
404bf8d2dd2SNiklas Schnelle 	if (zdev->s390_domain)
405c76c067eSNiklas Schnelle 		s390_iommu_detach_device(&zdev->s390_domain->domain, dev);
406bf8d2dd2SNiklas Schnelle 
407bf8d2dd2SNiklas Schnelle 	cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
40859bbf596SNiklas Schnelle 				virt_to_phys(s390_domain->dma_table), &status);
40959bbf596SNiklas Schnelle 	/*
41059bbf596SNiklas Schnelle 	 * If the device is undergoing error recovery the reset code
41159bbf596SNiklas Schnelle 	 * will re-establish the new domain.
41259bbf596SNiklas Schnelle 	 */
41359bbf596SNiklas Schnelle 	if (cc && status != ZPCI_PCI_ST_FUNC_NOT_AVAIL)
4141a3a7d64SNiklas Schnelle 		return -EIO;
4158128f23cSGerald Schaefer 
416cbf7827bSNiklas Schnelle 	zdev->dma_table = s390_domain->dma_table;
4178128f23cSGerald Schaefer 	zdev->s390_domain = s390_domain;
418cbf7827bSNiklas Schnelle 
419cbf7827bSNiklas Schnelle 	spin_lock_irqsave(&s390_domain->list_lock, flags);
4202ba8336dSNiklas Schnelle 	list_add_rcu(&zdev->iommu_list, &s390_domain->devices);
4218128f23cSGerald Schaefer 	spin_unlock_irqrestore(&s390_domain->list_lock, flags);
4228128f23cSGerald Schaefer 
4238128f23cSGerald Schaefer 	return 0;
4248128f23cSGerald Schaefer }
4258128f23cSGerald Schaefer 
s390_iommu_get_resv_regions(struct device * dev,struct list_head * list)426cbf7827bSNiklas Schnelle static void s390_iommu_get_resv_regions(struct device *dev,
427cbf7827bSNiklas Schnelle 					struct list_head *list)
428cbf7827bSNiklas Schnelle {
429cbf7827bSNiklas Schnelle 	struct zpci_dev *zdev = to_zpci_dev(dev);
430cbf7827bSNiklas Schnelle 	struct iommu_resv_region *region;
431cbf7827bSNiklas Schnelle 
432cbf7827bSNiklas Schnelle 	if (zdev->start_dma) {
433cbf7827bSNiklas Schnelle 		region = iommu_alloc_resv_region(0, zdev->start_dma, 0,
434cbf7827bSNiklas Schnelle 						 IOMMU_RESV_RESERVED, GFP_KERNEL);
435cbf7827bSNiklas Schnelle 		if (!region)
436cbf7827bSNiklas Schnelle 			return;
437cbf7827bSNiklas Schnelle 		list_add_tail(&region->list, list);
438cbf7827bSNiklas Schnelle 	}
439cbf7827bSNiklas Schnelle 
440cbf7827bSNiklas Schnelle 	if (zdev->end_dma < ZPCI_TABLE_SIZE_RT - 1) {
441cbf7827bSNiklas Schnelle 		region = iommu_alloc_resv_region(zdev->end_dma + 1,
442cbf7827bSNiklas Schnelle 						 ZPCI_TABLE_SIZE_RT - zdev->end_dma - 1,
443cbf7827bSNiklas Schnelle 						 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
444cbf7827bSNiklas Schnelle 		if (!region)
445cbf7827bSNiklas Schnelle 			return;
446cbf7827bSNiklas Schnelle 		list_add_tail(&region->list, list);
447cbf7827bSNiklas Schnelle 	}
448cbf7827bSNiklas Schnelle }
449cbf7827bSNiklas Schnelle 
s390_iommu_probe_device(struct device * dev)450522af649SJoerg Roedel static struct iommu_device *s390_iommu_probe_device(struct device *dev)
4518128f23cSGerald Schaefer {
452927a5fddSMatthew Rosato 	struct zpci_dev *zdev;
453927a5fddSMatthew Rosato 
454927a5fddSMatthew Rosato 	if (!dev_is_pci(dev))
455927a5fddSMatthew Rosato 		return ERR_PTR(-ENODEV);
456927a5fddSMatthew Rosato 
457927a5fddSMatthew Rosato 	zdev = to_zpci_dev(dev);
4588128f23cSGerald Schaefer 
459cbf7827bSNiklas Schnelle 	if (zdev->start_dma > zdev->end_dma ||
460cbf7827bSNiklas Schnelle 	    zdev->start_dma > ZPCI_TABLE_SIZE_RT - 1)
461cbf7827bSNiklas Schnelle 		return ERR_PTR(-EINVAL);
462cbf7827bSNiklas Schnelle 
463cbf7827bSNiklas Schnelle 	if (zdev->end_dma > ZPCI_TABLE_SIZE_RT - 1)
464cbf7827bSNiklas Schnelle 		zdev->end_dma = ZPCI_TABLE_SIZE_RT - 1;
465cbf7827bSNiklas Schnelle 
466*32d5bc8bSNiklas Schnelle 	if (zdev->tlb_refresh)
467*32d5bc8bSNiklas Schnelle 		dev->iommu->shadow_on_flush = 1;
468*32d5bc8bSNiklas Schnelle 
469522af649SJoerg Roedel 	return &zdev->iommu_dev;
4708128f23cSGerald Schaefer }
4718128f23cSGerald Schaefer 
s390_iommu_release_device(struct device * dev)472522af649SJoerg Roedel static void s390_iommu_release_device(struct device *dev)
4738128f23cSGerald Schaefer {
474d08d6f5dSPierre Morel 	struct zpci_dev *zdev = to_zpci_dev(dev);
4758128f23cSGerald Schaefer 
4768128f23cSGerald Schaefer 	/*
477bf8d2dd2SNiklas Schnelle 	 * release_device is expected to detach any domain currently attached
478bf8d2dd2SNiklas Schnelle 	 * to the device, but keep it attached to other devices in the group.
4798128f23cSGerald Schaefer 	 */
480bf8d2dd2SNiklas Schnelle 	if (zdev)
481c76c067eSNiklas Schnelle 		s390_iommu_detach_device(&zdev->s390_domain->domain, dev);
4828128f23cSGerald Schaefer }
4838128f23cSGerald Schaefer 
zpci_refresh_all(struct zpci_dev * zdev)484fa4c4507SNiklas Schnelle static int zpci_refresh_all(struct zpci_dev *zdev)
485fa4c4507SNiklas Schnelle {
486fa4c4507SNiklas Schnelle 	return zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma,
487fa4c4507SNiklas Schnelle 				  zdev->end_dma - zdev->start_dma + 1);
488fa4c4507SNiklas Schnelle }
489fa4c4507SNiklas Schnelle 
s390_iommu_flush_iotlb_all(struct iommu_domain * domain)490c228f5a0SNiklas Schnelle static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain)
491c228f5a0SNiklas Schnelle {
492c228f5a0SNiklas Schnelle 	struct s390_domain *s390_domain = to_s390_domain(domain);
493c228f5a0SNiklas Schnelle 	struct zpci_dev *zdev;
494c228f5a0SNiklas Schnelle 
4952ba8336dSNiklas Schnelle 	rcu_read_lock();
4962ba8336dSNiklas Schnelle 	list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
497c76c067eSNiklas Schnelle 		atomic64_inc(&s390_domain->ctrs.global_rpcits);
498fa4c4507SNiklas Schnelle 		zpci_refresh_all(zdev);
499c228f5a0SNiklas Schnelle 	}
5002ba8336dSNiklas Schnelle 	rcu_read_unlock();
501c228f5a0SNiklas Schnelle }
502c228f5a0SNiklas Schnelle 
s390_iommu_iotlb_sync(struct iommu_domain * domain,struct iommu_iotlb_gather * gather)503c228f5a0SNiklas Schnelle static void s390_iommu_iotlb_sync(struct iommu_domain *domain,
504c228f5a0SNiklas Schnelle 				  struct iommu_iotlb_gather *gather)
505c228f5a0SNiklas Schnelle {
506c228f5a0SNiklas Schnelle 	struct s390_domain *s390_domain = to_s390_domain(domain);
507c228f5a0SNiklas Schnelle 	size_t size = gather->end - gather->start + 1;
508c228f5a0SNiklas Schnelle 	struct zpci_dev *zdev;
509c228f5a0SNiklas Schnelle 
510c228f5a0SNiklas Schnelle 	/* If gather was never added to there is nothing to flush */
511c228f5a0SNiklas Schnelle 	if (!gather->end)
512c228f5a0SNiklas Schnelle 		return;
513c228f5a0SNiklas Schnelle 
5142ba8336dSNiklas Schnelle 	rcu_read_lock();
5152ba8336dSNiklas Schnelle 	list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
516c76c067eSNiklas Schnelle 		atomic64_inc(&s390_domain->ctrs.sync_rpcits);
517c228f5a0SNiklas Schnelle 		zpci_refresh_trans((u64)zdev->fh << 32, gather->start,
518c228f5a0SNiklas Schnelle 				   size);
519c228f5a0SNiklas Schnelle 	}
5202ba8336dSNiklas Schnelle 	rcu_read_unlock();
521c228f5a0SNiklas Schnelle }
522c228f5a0SNiklas Schnelle 
s390_iommu_iotlb_sync_map(struct iommu_domain * domain,unsigned long iova,size_t size)523fa4c4507SNiklas Schnelle static int s390_iommu_iotlb_sync_map(struct iommu_domain *domain,
524c228f5a0SNiklas Schnelle 				     unsigned long iova, size_t size)
525c228f5a0SNiklas Schnelle {
526c228f5a0SNiklas Schnelle 	struct s390_domain *s390_domain = to_s390_domain(domain);
527c228f5a0SNiklas Schnelle 	struct zpci_dev *zdev;
528fa4c4507SNiklas Schnelle 	int ret = 0;
529c228f5a0SNiklas Schnelle 
5302ba8336dSNiklas Schnelle 	rcu_read_lock();
5312ba8336dSNiklas Schnelle 	list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
532c228f5a0SNiklas Schnelle 		if (!zdev->tlb_refresh)
533c228f5a0SNiklas Schnelle 			continue;
534c76c067eSNiklas Schnelle 		atomic64_inc(&s390_domain->ctrs.sync_map_rpcits);
535fa4c4507SNiklas Schnelle 		ret = zpci_refresh_trans((u64)zdev->fh << 32,
536c228f5a0SNiklas Schnelle 					 iova, size);
537fa4c4507SNiklas Schnelle 		/*
538fa4c4507SNiklas Schnelle 		 * let the hypervisor discover invalidated entries
539fa4c4507SNiklas Schnelle 		 * allowing it to free IOVAs and unpin pages
540fa4c4507SNiklas Schnelle 		 */
541fa4c4507SNiklas Schnelle 		if (ret == -ENOMEM) {
542fa4c4507SNiklas Schnelle 			ret = zpci_refresh_all(zdev);
543fa4c4507SNiklas Schnelle 			if (ret)
544fa4c4507SNiklas Schnelle 				break;
545fa4c4507SNiklas Schnelle 		}
546c228f5a0SNiklas Schnelle 	}
5472ba8336dSNiklas Schnelle 	rcu_read_unlock();
548fa4c4507SNiklas Schnelle 
549fa4c4507SNiklas Schnelle 	return ret;
550c228f5a0SNiklas Schnelle }
551c228f5a0SNiklas Schnelle 
s390_iommu_validate_trans(struct s390_domain * s390_domain,phys_addr_t pa,dma_addr_t dma_addr,unsigned long nr_pages,int flags,gfp_t gfp)55208955af0SNiklas Schnelle static int s390_iommu_validate_trans(struct s390_domain *s390_domain,
553568de506SNiklas Schnelle 				     phys_addr_t pa, dma_addr_t dma_addr,
554d3b82825SJason Gunthorpe 				     unsigned long nr_pages, int flags,
555d3b82825SJason Gunthorpe 				     gfp_t gfp)
5568128f23cSGerald Schaefer {
557568de506SNiklas Schnelle 	phys_addr_t page_addr = pa & PAGE_MASK;
55866728eeeSSebastian Ott 	unsigned long *entry;
55921c1f902SNiklas Schnelle 	unsigned long i;
56008955af0SNiklas Schnelle 	int rc;
56108955af0SNiklas Schnelle 
56208955af0SNiklas Schnelle 	for (i = 0; i < nr_pages; i++) {
563d3b82825SJason Gunthorpe 		entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr,
564d3b82825SJason Gunthorpe 					   gfp);
56508955af0SNiklas Schnelle 		if (unlikely(!entry)) {
56608955af0SNiklas Schnelle 			rc = -ENOMEM;
56708955af0SNiklas Schnelle 			goto undo_cpu_trans;
56808955af0SNiklas Schnelle 		}
56908955af0SNiklas Schnelle 		dma_update_cpu_trans(entry, page_addr, flags);
57008955af0SNiklas Schnelle 		page_addr += PAGE_SIZE;
57108955af0SNiklas Schnelle 		dma_addr += PAGE_SIZE;
57208955af0SNiklas Schnelle 	}
57308955af0SNiklas Schnelle 
57408955af0SNiklas Schnelle 	return 0;
57508955af0SNiklas Schnelle 
57608955af0SNiklas Schnelle undo_cpu_trans:
57708955af0SNiklas Schnelle 	while (i-- > 0) {
57808955af0SNiklas Schnelle 		dma_addr -= PAGE_SIZE;
57908955af0SNiklas Schnelle 		entry = dma_walk_cpu_trans(s390_domain->dma_table,
580d3b82825SJason Gunthorpe 					   dma_addr, gfp);
58108955af0SNiklas Schnelle 		if (!entry)
58208955af0SNiklas Schnelle 			break;
58308955af0SNiklas Schnelle 		dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
58408955af0SNiklas Schnelle 	}
58508955af0SNiklas Schnelle 
58608955af0SNiklas Schnelle 	return rc;
58708955af0SNiklas Schnelle }
58808955af0SNiklas Schnelle 
s390_iommu_invalidate_trans(struct s390_domain * s390_domain,dma_addr_t dma_addr,unsigned long nr_pages)58908955af0SNiklas Schnelle static int s390_iommu_invalidate_trans(struct s390_domain *s390_domain,
59008955af0SNiklas Schnelle 				       dma_addr_t dma_addr, unsigned long nr_pages)
59108955af0SNiklas Schnelle {
59208955af0SNiklas Schnelle 	unsigned long *entry;
59321c1f902SNiklas Schnelle 	unsigned long i;
5948128f23cSGerald Schaefer 	int rc = 0;
5958128f23cSGerald Schaefer 
5968128f23cSGerald Schaefer 	for (i = 0; i < nr_pages; i++) {
597d3b82825SJason Gunthorpe 		entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr,
598d3b82825SJason Gunthorpe 					   GFP_ATOMIC);
59908955af0SNiklas Schnelle 		if (unlikely(!entry)) {
60008955af0SNiklas Schnelle 			rc = -EINVAL;
60166728eeeSSebastian Ott 			break;
60266728eeeSSebastian Ott 		}
60308955af0SNiklas Schnelle 		dma_update_cpu_trans(entry, 0, ZPCI_PTE_INVALID);
60408955af0SNiklas Schnelle 		dma_addr += PAGE_SIZE;
60566728eeeSSebastian Ott 	}
6068128f23cSGerald Schaefer 
6078128f23cSGerald Schaefer 	return rc;
6088128f23cSGerald Schaefer }
6098128f23cSGerald Schaefer 
s390_iommu_map_pages(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t pgsize,size_t pgcount,int prot,gfp_t gfp,size_t * mapped)610f3cc4f87SNiklas Schnelle static int s390_iommu_map_pages(struct iommu_domain *domain,
611f3cc4f87SNiklas Schnelle 				unsigned long iova, phys_addr_t paddr,
612f3cc4f87SNiklas Schnelle 				size_t pgsize, size_t pgcount,
613f3cc4f87SNiklas Schnelle 				int prot, gfp_t gfp, size_t *mapped)
6148128f23cSGerald Schaefer {
6158128f23cSGerald Schaefer 	struct s390_domain *s390_domain = to_s390_domain(domain);
616f3cc4f87SNiklas Schnelle 	size_t size = pgcount << __ffs(pgsize);
61708955af0SNiklas Schnelle 	int flags = ZPCI_PTE_VALID, rc = 0;
618f3cc4f87SNiklas Schnelle 
619f3cc4f87SNiklas Schnelle 	if (pgsize != SZ_4K)
620f3cc4f87SNiklas Schnelle 		return -EINVAL;
621f3cc4f87SNiklas Schnelle 
622f3cc4f87SNiklas Schnelle 	if (iova < s390_domain->domain.geometry.aperture_start ||
623f3cc4f87SNiklas Schnelle 	    (iova + size - 1) > s390_domain->domain.geometry.aperture_end)
624f3cc4f87SNiklas Schnelle 		return -EINVAL;
625f3cc4f87SNiklas Schnelle 
626f3cc4f87SNiklas Schnelle 	if (!IS_ALIGNED(iova | paddr, pgsize))
627f3cc4f87SNiklas Schnelle 		return -EINVAL;
6288128f23cSGerald Schaefer 
6298128f23cSGerald Schaefer 	if (!(prot & IOMMU_WRITE))
6308128f23cSGerald Schaefer 		flags |= ZPCI_TABLE_PROTECTED;
6318128f23cSGerald Schaefer 
63208955af0SNiklas Schnelle 	rc = s390_iommu_validate_trans(s390_domain, paddr, iova,
633d3b82825SJason Gunthorpe 				     pgcount, flags, gfp);
634c76c067eSNiklas Schnelle 	if (!rc) {
635f3cc4f87SNiklas Schnelle 		*mapped = size;
636c76c067eSNiklas Schnelle 		atomic64_add(pgcount, &s390_domain->ctrs.mapped_pages);
637c76c067eSNiklas Schnelle 	}
6388128f23cSGerald Schaefer 
6398128f23cSGerald Schaefer 	return rc;
6408128f23cSGerald Schaefer }
6418128f23cSGerald Schaefer 
s390_iommu_iova_to_phys(struct iommu_domain * domain,dma_addr_t iova)6428128f23cSGerald Schaefer static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
6438128f23cSGerald Schaefer 					   dma_addr_t iova)
6448128f23cSGerald Schaefer {
6458128f23cSGerald Schaefer 	struct s390_domain *s390_domain = to_s390_domain(domain);
64621c1f902SNiklas Schnelle 	unsigned long *rto, *sto, *pto;
64721c1f902SNiklas Schnelle 	unsigned long ste, pte, rte;
6488128f23cSGerald Schaefer 	unsigned int rtx, sx, px;
6498128f23cSGerald Schaefer 	phys_addr_t phys = 0;
6508128f23cSGerald Schaefer 
6518128f23cSGerald Schaefer 	if (iova < domain->geometry.aperture_start ||
6528128f23cSGerald Schaefer 	    iova > domain->geometry.aperture_end)
6538128f23cSGerald Schaefer 		return 0;
6548128f23cSGerald Schaefer 
6558128f23cSGerald Schaefer 	rtx = calc_rtx(iova);
6568128f23cSGerald Schaefer 	sx = calc_sx(iova);
6578128f23cSGerald Schaefer 	px = calc_px(iova);
6588128f23cSGerald Schaefer 	rto = s390_domain->dma_table;
6598128f23cSGerald Schaefer 
66021c1f902SNiklas Schnelle 	rte = READ_ONCE(rto[rtx]);
66121c1f902SNiklas Schnelle 	if (reg_entry_isvalid(rte)) {
66221c1f902SNiklas Schnelle 		sto = get_rt_sto(rte);
66321c1f902SNiklas Schnelle 		ste = READ_ONCE(sto[sx]);
66421c1f902SNiklas Schnelle 		if (reg_entry_isvalid(ste)) {
66521c1f902SNiklas Schnelle 			pto = get_st_pto(ste);
66621c1f902SNiklas Schnelle 			pte = READ_ONCE(pto[px]);
66721c1f902SNiklas Schnelle 			if (pt_entry_isvalid(pte))
66821c1f902SNiklas Schnelle 				phys = pte & ZPCI_PTE_ADDR_MASK;
6698128f23cSGerald Schaefer 		}
6708128f23cSGerald Schaefer 	}
6718128f23cSGerald Schaefer 
6728128f23cSGerald Schaefer 	return phys;
6738128f23cSGerald Schaefer }
6748128f23cSGerald Schaefer 
s390_iommu_unmap_pages(struct iommu_domain * domain,unsigned long iova,size_t pgsize,size_t pgcount,struct iommu_iotlb_gather * gather)675f3cc4f87SNiklas Schnelle static size_t s390_iommu_unmap_pages(struct iommu_domain *domain,
676f3cc4f87SNiklas Schnelle 				     unsigned long iova,
677f3cc4f87SNiklas Schnelle 				     size_t pgsize, size_t pgcount,
67856f8af5eSWill Deacon 				     struct iommu_iotlb_gather *gather)
6798128f23cSGerald Schaefer {
6808128f23cSGerald Schaefer 	struct s390_domain *s390_domain = to_s390_domain(domain);
681f3cc4f87SNiklas Schnelle 	size_t size = pgcount << __ffs(pgsize);
6828128f23cSGerald Schaefer 	int rc;
6838128f23cSGerald Schaefer 
684f3cc4f87SNiklas Schnelle 	if (WARN_ON(iova < s390_domain->domain.geometry.aperture_start ||
685f3cc4f87SNiklas Schnelle 	    (iova + size - 1) > s390_domain->domain.geometry.aperture_end))
686f3cc4f87SNiklas Schnelle 		return 0;
687f3cc4f87SNiklas Schnelle 
68808955af0SNiklas Schnelle 	rc = s390_iommu_invalidate_trans(s390_domain, iova, pgcount);
6898128f23cSGerald Schaefer 	if (rc)
6908128f23cSGerald Schaefer 		return 0;
6918128f23cSGerald Schaefer 
692c228f5a0SNiklas Schnelle 	iommu_iotlb_gather_add_range(gather, iova, size);
693c76c067eSNiklas Schnelle 	atomic64_add(pgcount, &s390_domain->ctrs.unmapped_pages);
694c228f5a0SNiklas Schnelle 
6958128f23cSGerald Schaefer 	return size;
6968128f23cSGerald Schaefer }
6978128f23cSGerald Schaefer 
zpci_get_iommu_ctrs(struct zpci_dev * zdev)698c76c067eSNiklas Schnelle struct zpci_iommu_ctrs *zpci_get_iommu_ctrs(struct zpci_dev *zdev)
699c76c067eSNiklas Schnelle {
700c76c067eSNiklas Schnelle 	if (!zdev || !zdev->s390_domain)
701c76c067eSNiklas Schnelle 		return NULL;
702c76c067eSNiklas Schnelle 	return &zdev->s390_domain->ctrs;
703c76c067eSNiklas Schnelle }
704c76c067eSNiklas Schnelle 
zpci_init_iommu(struct zpci_dev * zdev)705f42c2235SJoerg Roedel int zpci_init_iommu(struct zpci_dev *zdev)
706f42c2235SJoerg Roedel {
707c76c067eSNiklas Schnelle 	u64 aperture_size;
708f42c2235SJoerg Roedel 	int rc = 0;
709f42c2235SJoerg Roedel 
710f42c2235SJoerg Roedel 	rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
711f42c2235SJoerg Roedel 				    "s390-iommu.%08x", zdev->fid);
712f42c2235SJoerg Roedel 	if (rc)
713f42c2235SJoerg Roedel 		goto out_err;
714f42c2235SJoerg Roedel 
7152d471b20SRobin Murphy 	rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
716f42c2235SJoerg Roedel 	if (rc)
717f42c2235SJoerg Roedel 		goto out_sysfs;
718f42c2235SJoerg Roedel 
719c76c067eSNiklas Schnelle 	zdev->start_dma = PAGE_ALIGN(zdev->start_dma);
720c76c067eSNiklas Schnelle 	aperture_size = min3(s390_iommu_aperture,
721c76c067eSNiklas Schnelle 			     ZPCI_TABLE_SIZE_RT - zdev->start_dma,
722c76c067eSNiklas Schnelle 			     zdev->end_dma - zdev->start_dma + 1);
723c76c067eSNiklas Schnelle 	zdev->end_dma = zdev->start_dma + aperture_size - 1;
724c76c067eSNiklas Schnelle 
725f42c2235SJoerg Roedel 	return 0;
726f42c2235SJoerg Roedel 
727f42c2235SJoerg Roedel out_sysfs:
728f42c2235SJoerg Roedel 	iommu_device_sysfs_remove(&zdev->iommu_dev);
729f42c2235SJoerg Roedel 
730f42c2235SJoerg Roedel out_err:
731f42c2235SJoerg Roedel 	return rc;
732f42c2235SJoerg Roedel }
733f42c2235SJoerg Roedel 
zpci_destroy_iommu(struct zpci_dev * zdev)734f42c2235SJoerg Roedel void zpci_destroy_iommu(struct zpci_dev *zdev)
735f42c2235SJoerg Roedel {
736f42c2235SJoerg Roedel 	iommu_device_unregister(&zdev->iommu_dev);
737f42c2235SJoerg Roedel 	iommu_device_sysfs_remove(&zdev->iommu_dev);
738f42c2235SJoerg Roedel }
739f42c2235SJoerg Roedel 
s390_iommu_setup(char * str)740c76c067eSNiklas Schnelle static int __init s390_iommu_setup(char *str)
741c76c067eSNiklas Schnelle {
742c76c067eSNiklas Schnelle 	if (!strcmp(str, "strict")) {
743c76c067eSNiklas Schnelle 		pr_warn("s390_iommu=strict deprecated; use iommu.strict=1 instead\n");
744c76c067eSNiklas Schnelle 		iommu_set_dma_strict();
745c76c067eSNiklas Schnelle 	}
746c76c067eSNiklas Schnelle 	return 1;
747c76c067eSNiklas Schnelle }
748c76c067eSNiklas Schnelle 
749c76c067eSNiklas Schnelle __setup("s390_iommu=", s390_iommu_setup);
750c76c067eSNiklas Schnelle 
s390_iommu_aperture_setup(char * str)751c76c067eSNiklas Schnelle static int __init s390_iommu_aperture_setup(char *str)
752c76c067eSNiklas Schnelle {
753c76c067eSNiklas Schnelle 	if (kstrtou32(str, 10, &s390_iommu_aperture_factor))
754c76c067eSNiklas Schnelle 		s390_iommu_aperture_factor = 1;
755c76c067eSNiklas Schnelle 	return 1;
756c76c067eSNiklas Schnelle }
757c76c067eSNiklas Schnelle 
758c76c067eSNiklas Schnelle __setup("s390_iommu_aperture=", s390_iommu_aperture_setup);
759c76c067eSNiklas Schnelle 
s390_iommu_init(void)760c76c067eSNiklas Schnelle static int __init s390_iommu_init(void)
761c76c067eSNiklas Schnelle {
762c76c067eSNiklas Schnelle 	int rc;
763c76c067eSNiklas Schnelle 
764c76c067eSNiklas Schnelle 	iommu_dma_forcedac = true;
765c76c067eSNiklas Schnelle 	s390_iommu_aperture = (u64)virt_to_phys(high_memory);
766c76c067eSNiklas Schnelle 	if (!s390_iommu_aperture_factor)
767c76c067eSNiklas Schnelle 		s390_iommu_aperture = ULONG_MAX;
768c76c067eSNiklas Schnelle 	else
769c76c067eSNiklas Schnelle 		s390_iommu_aperture *= s390_iommu_aperture_factor;
770c76c067eSNiklas Schnelle 
771c76c067eSNiklas Schnelle 	rc = dma_alloc_cpu_table_caches();
772c76c067eSNiklas Schnelle 	if (rc)
773c76c067eSNiklas Schnelle 		return rc;
774c76c067eSNiklas Schnelle 
775c76c067eSNiklas Schnelle 	return rc;
776c76c067eSNiklas Schnelle }
777c76c067eSNiklas Schnelle subsys_initcall(s390_iommu_init);
778c76c067eSNiklas Schnelle 
779cceb8451SArvind Yadav static const struct iommu_ops s390_iommu_ops = {
7808128f23cSGerald Schaefer 	.capable = s390_iommu_capable,
7814efd98d4SJason Gunthorpe 	.domain_alloc_paging = s390_domain_alloc_paging,
7829a630a4bSLu Baolu 	.probe_device = s390_iommu_probe_device,
7839a630a4bSLu Baolu 	.release_device = s390_iommu_release_device,
7849a630a4bSLu Baolu 	.device_group = generic_device_group,
785b4d8ae0eSNiklas Schnelle 	.pgsize_bitmap = SZ_4K,
786cbf7827bSNiklas Schnelle 	.get_resv_regions = s390_iommu_get_resv_regions,
7879a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
7888128f23cSGerald Schaefer 		.attach_dev	= s390_iommu_attach_device,
789f3cc4f87SNiklas Schnelle 		.map_pages	= s390_iommu_map_pages,
790f3cc4f87SNiklas Schnelle 		.unmap_pages	= s390_iommu_unmap_pages,
791c228f5a0SNiklas Schnelle 		.flush_iotlb_all = s390_iommu_flush_iotlb_all,
792c228f5a0SNiklas Schnelle 		.iotlb_sync      = s390_iommu_iotlb_sync,
793c228f5a0SNiklas Schnelle 		.iotlb_sync_map  = s390_iommu_iotlb_sync_map,
7948128f23cSGerald Schaefer 		.iova_to_phys	= s390_iommu_iova_to_phys,
7959a630a4bSLu Baolu 		.free		= s390_domain_free,
7969a630a4bSLu Baolu 	}
7978128f23cSGerald Schaefer };
798