2 * IOMMU API for s390 PCI devices
4 * Copyright IBM Corp. 2015
5 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
9 #include <linux/iommu.h>
10 #include <linux/iommu-helper.h>
11 #include <linux/pci.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)
23 struct iommu_domain domain
;
24 struct list_head devices
;
25 unsigned long *dma_table
;
26 spinlock_t dma_table_lock
;
30 struct s390_domain_device
{
31 struct list_head list
;
32 struct zpci_dev
*zdev
;
35 static struct s390_domain
*to_s390_domain(struct iommu_domain
*dom
)
37 return container_of(dom
, struct s390_domain
, domain
);
40 static bool s390_iommu_capable(enum iommu_cap cap
)
43 case IOMMU_CAP_CACHE_COHERENCY
:
45 case IOMMU_CAP_INTR_REMAP
:
52 static struct iommu_domain
*s390_domain_alloc(unsigned domain_type
)
54 struct s390_domain
*s390_domain
;
56 if (domain_type
!= IOMMU_DOMAIN_UNMANAGED
)
59 s390_domain
= kzalloc(sizeof(*s390_domain
), GFP_KERNEL
);
63 s390_domain
->dma_table
= dma_alloc_cpu_table();
64 if (!s390_domain
->dma_table
) {
69 spin_lock_init(&s390_domain
->dma_table_lock
);
70 spin_lock_init(&s390_domain
->list_lock
);
71 INIT_LIST_HEAD(&s390_domain
->devices
);
73 return &s390_domain
->domain
;
76 static void s390_domain_free(struct iommu_domain
*domain
)
78 struct s390_domain
*s390_domain
= to_s390_domain(domain
);
80 dma_cleanup_tables(s390_domain
->dma_table
);
84 static int s390_iommu_attach_device(struct iommu_domain
*domain
,
87 struct s390_domain
*s390_domain
= to_s390_domain(domain
);
88 struct zpci_dev
*zdev
= to_pci_dev(dev
)->sysdata
;
89 struct s390_domain_device
*domain_device
;
96 domain_device
= kzalloc(sizeof(*domain_device
), GFP_KERNEL
);
101 zpci_dma_exit_device(zdev
);
103 zdev
->dma_table
= s390_domain
->dma_table
;
104 rc
= zpci_register_ioat(zdev
, 0, zdev
->start_dma
, zdev
->end_dma
,
105 (u64
) zdev
->dma_table
);
109 spin_lock_irqsave(&s390_domain
->list_lock
, flags
);
110 /* First device defines the DMA range limits */
111 if (list_empty(&s390_domain
->devices
)) {
112 domain
->geometry
.aperture_start
= zdev
->start_dma
;
113 domain
->geometry
.aperture_end
= zdev
->end_dma
;
114 domain
->geometry
.force_aperture
= true;
115 /* Allow only devices with identical DMA range limits */
116 } else if (domain
->geometry
.aperture_start
!= zdev
->start_dma
||
117 domain
->geometry
.aperture_end
!= zdev
->end_dma
) {
119 spin_unlock_irqrestore(&s390_domain
->list_lock
, flags
);
122 domain_device
->zdev
= zdev
;
123 zdev
->s390_domain
= s390_domain
;
124 list_add(&domain_device
->list
, &s390_domain
->devices
);
125 spin_unlock_irqrestore(&s390_domain
->list_lock
, flags
);
130 zpci_dma_init_device(zdev
);
131 kfree(domain_device
);
136 static void s390_iommu_detach_device(struct iommu_domain
*domain
,
139 struct s390_domain
*s390_domain
= to_s390_domain(domain
);
140 struct zpci_dev
*zdev
= to_pci_dev(dev
)->sysdata
;
141 struct s390_domain_device
*domain_device
, *tmp
;
148 spin_lock_irqsave(&s390_domain
->list_lock
, flags
);
149 list_for_each_entry_safe(domain_device
, tmp
, &s390_domain
->devices
,
151 if (domain_device
->zdev
== zdev
) {
152 list_del(&domain_device
->list
);
153 kfree(domain_device
);
158 spin_unlock_irqrestore(&s390_domain
->list_lock
, flags
);
161 zdev
->s390_domain
= NULL
;
162 zpci_unregister_ioat(zdev
, 0);
163 zpci_dma_init_device(zdev
);
167 static int s390_iommu_add_device(struct device
*dev
)
169 struct iommu_group
*group
;
172 group
= iommu_group_get(dev
);
174 group
= iommu_group_alloc();
176 return PTR_ERR(group
);
179 rc
= iommu_group_add_device(group
, dev
);
180 iommu_group_put(group
);
185 static void s390_iommu_remove_device(struct device
*dev
)
187 struct zpci_dev
*zdev
= to_pci_dev(dev
)->sysdata
;
188 struct iommu_domain
*domain
;
191 * This is a workaround for a scenario where the IOMMU API common code
192 * "forgets" to call the detach_dev callback: After binding a device
193 * to vfio-pci and completing the VFIO_SET_IOMMU ioctl (which triggers
194 * the attach_dev), removing the device via
195 * "echo 1 > /sys/bus/pci/devices/.../remove" won't trigger detach_dev,
196 * only remove_device will be called via the BUS_NOTIFY_REMOVED_DEVICE
199 * So let's call detach_dev from here if it hasn't been called before.
201 if (zdev
&& zdev
->s390_domain
) {
202 domain
= iommu_get_domain_for_dev(dev
);
204 s390_iommu_detach_device(domain
, dev
);
207 iommu_group_remove_device(dev
);
210 static int s390_iommu_update_trans(struct s390_domain
*s390_domain
,
211 unsigned long pa
, dma_addr_t dma_addr
,
212 size_t size
, int flags
)
214 struct s390_domain_device
*domain_device
;
215 u8
*page_addr
= (u8
*) (pa
& PAGE_MASK
);
216 dma_addr_t start_dma_addr
= dma_addr
;
217 unsigned long irq_flags
, nr_pages
, i
;
218 unsigned long *entry
;
221 if (dma_addr
< s390_domain
->domain
.geometry
.aperture_start
||
222 dma_addr
+ size
> s390_domain
->domain
.geometry
.aperture_end
)
225 nr_pages
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
229 spin_lock_irqsave(&s390_domain
->dma_table_lock
, irq_flags
);
230 for (i
= 0; i
< nr_pages
; i
++) {
231 entry
= dma_walk_cpu_trans(s390_domain
->dma_table
, dma_addr
);
236 dma_update_cpu_trans(entry
, page_addr
, flags
);
237 page_addr
+= PAGE_SIZE
;
238 dma_addr
+= PAGE_SIZE
;
241 spin_lock(&s390_domain
->list_lock
);
242 list_for_each_entry(domain_device
, &s390_domain
->devices
, list
) {
243 rc
= zpci_refresh_trans((u64
) domain_device
->zdev
->fh
<< 32,
244 start_dma_addr
, nr_pages
* PAGE_SIZE
);
248 spin_unlock(&s390_domain
->list_lock
);
251 if (rc
&& ((flags
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
)) {
252 flags
= ZPCI_PTE_INVALID
;
254 page_addr
-= PAGE_SIZE
;
255 dma_addr
-= PAGE_SIZE
;
256 entry
= dma_walk_cpu_trans(s390_domain
->dma_table
,
260 dma_update_cpu_trans(entry
, page_addr
, flags
);
263 spin_unlock_irqrestore(&s390_domain
->dma_table_lock
, irq_flags
);
268 static int s390_iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
269 phys_addr_t paddr
, size_t size
, int prot
)
271 struct s390_domain
*s390_domain
= to_s390_domain(domain
);
272 int flags
= ZPCI_PTE_VALID
, rc
= 0;
274 if (!(prot
& IOMMU_READ
))
277 if (!(prot
& IOMMU_WRITE
))
278 flags
|= ZPCI_TABLE_PROTECTED
;
280 rc
= s390_iommu_update_trans(s390_domain
, (unsigned long) paddr
, iova
,
286 static phys_addr_t
s390_iommu_iova_to_phys(struct iommu_domain
*domain
,
289 struct s390_domain
*s390_domain
= to_s390_domain(domain
);
290 unsigned long *sto
, *pto
, *rto
, flags
;
291 unsigned int rtx
, sx
, px
;
292 phys_addr_t phys
= 0;
294 if (iova
< domain
->geometry
.aperture_start
||
295 iova
> domain
->geometry
.aperture_end
)
298 rtx
= calc_rtx(iova
);
301 rto
= s390_domain
->dma_table
;
303 spin_lock_irqsave(&s390_domain
->dma_table_lock
, flags
);
304 if (rto
&& reg_entry_isvalid(rto
[rtx
])) {
305 sto
= get_rt_sto(rto
[rtx
]);
306 if (sto
&& reg_entry_isvalid(sto
[sx
])) {
307 pto
= get_st_pto(sto
[sx
]);
308 if (pto
&& pt_entry_isvalid(pto
[px
]))
309 phys
= pto
[px
] & ZPCI_PTE_ADDR_MASK
;
312 spin_unlock_irqrestore(&s390_domain
->dma_table_lock
, flags
);
317 static size_t s390_iommu_unmap(struct iommu_domain
*domain
,
318 unsigned long iova
, size_t size
)
320 struct s390_domain
*s390_domain
= to_s390_domain(domain
);
321 int flags
= ZPCI_PTE_INVALID
;
325 paddr
= s390_iommu_iova_to_phys(domain
, iova
);
329 rc
= s390_iommu_update_trans(s390_domain
, (unsigned long) paddr
, iova
,
337 static struct iommu_ops s390_iommu_ops
= {
338 .capable
= s390_iommu_capable
,
339 .domain_alloc
= s390_domain_alloc
,
340 .domain_free
= s390_domain_free
,
341 .attach_dev
= s390_iommu_attach_device
,
342 .detach_dev
= s390_iommu_detach_device
,
343 .map
= s390_iommu_map
,
344 .unmap
= s390_iommu_unmap
,
345 .iova_to_phys
= s390_iommu_iova_to_phys
,
346 .add_device
= s390_iommu_add_device
,
347 .remove_device
= s390_iommu_remove_device
,
348 .pgsize_bitmap
= S390_IOMMU_PGSIZES
,
351 static int __init
s390_iommu_init(void)
353 return bus_set_iommu(&pci_bus_type
, &s390_iommu_ops
);
355 subsys_initcall(s390_iommu_init
);