xref: /openbmc/linux/arch/s390/pci/pci_dma.c (revision 844f5ed5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2012
4  *
5  * Author(s):
6  *   Jan Glauber <jang@linux.vnet.ibm.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
12 #include <linux/iommu-helper.h>
13 #include <linux/dma-map-ops.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pci.h>
16 #include <asm/pci_dma.h>
17 
18 static struct kmem_cache *dma_region_table_cache;
19 static struct kmem_cache *dma_page_table_cache;
20 static int s390_iommu_strict;
21 static u64 s390_iommu_aperture;
22 static u32 s390_iommu_aperture_factor = 1;
23 
24 static int zpci_refresh_global(struct zpci_dev *zdev)
25 {
26 	return zpci_refresh_trans((u64) zdev->fh << 32, zdev->start_dma,
27 				  zdev->iommu_pages * PAGE_SIZE);
28 }
29 
30 unsigned long *dma_alloc_cpu_table(gfp_t gfp)
31 {
32 	unsigned long *table, *entry;
33 
34 	table = kmem_cache_alloc(dma_region_table_cache, gfp);
35 	if (!table)
36 		return NULL;
37 
38 	for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
39 		*entry = ZPCI_TABLE_INVALID;
40 	return table;
41 }
42 
43 static void dma_free_cpu_table(void *table)
44 {
45 	kmem_cache_free(dma_region_table_cache, table);
46 }
47 
48 static unsigned long *dma_alloc_page_table(gfp_t gfp)
49 {
50 	unsigned long *table, *entry;
51 
52 	table = kmem_cache_alloc(dma_page_table_cache, gfp);
53 	if (!table)
54 		return NULL;
55 
56 	for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
57 		*entry = ZPCI_PTE_INVALID;
58 	return table;
59 }
60 
61 static void dma_free_page_table(void *table)
62 {
63 	kmem_cache_free(dma_page_table_cache, table);
64 }
65 
66 static unsigned long *dma_get_seg_table_origin(unsigned long *rtep, gfp_t gfp)
67 {
68 	unsigned long old_rte, rte;
69 	unsigned long *sto;
70 
71 	rte = READ_ONCE(*rtep);
72 	if (reg_entry_isvalid(rte)) {
73 		sto = get_rt_sto(rte);
74 	} else {
75 		sto = dma_alloc_cpu_table(gfp);
76 		if (!sto)
77 			return NULL;
78 
79 		set_rt_sto(&rte, virt_to_phys(sto));
80 		validate_rt_entry(&rte);
81 		entry_clr_protected(&rte);
82 
83 		old_rte = cmpxchg(rtep, ZPCI_TABLE_INVALID, rte);
84 		if (old_rte != ZPCI_TABLE_INVALID) {
85 			/* Somone else was faster, use theirs */
86 			dma_free_cpu_table(sto);
87 			sto = get_rt_sto(old_rte);
88 		}
89 	}
90 	return sto;
91 }
92 
93 static unsigned long *dma_get_page_table_origin(unsigned long *step, gfp_t gfp)
94 {
95 	unsigned long old_ste, ste;
96 	unsigned long *pto;
97 
98 	ste = READ_ONCE(*step);
99 	if (reg_entry_isvalid(ste)) {
100 		pto = get_st_pto(ste);
101 	} else {
102 		pto = dma_alloc_page_table(gfp);
103 		if (!pto)
104 			return NULL;
105 		set_st_pto(&ste, virt_to_phys(pto));
106 		validate_st_entry(&ste);
107 		entry_clr_protected(&ste);
108 
109 		old_ste = cmpxchg(step, ZPCI_TABLE_INVALID, ste);
110 		if (old_ste != ZPCI_TABLE_INVALID) {
111 			/* Somone else was faster, use theirs */
112 			dma_free_page_table(pto);
113 			pto = get_st_pto(old_ste);
114 		}
115 	}
116 	return pto;
117 }
118 
119 unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr,
120 				  gfp_t gfp)
121 {
122 	unsigned long *sto, *pto;
123 	unsigned int rtx, sx, px;
124 
125 	rtx = calc_rtx(dma_addr);
126 	sto = dma_get_seg_table_origin(&rto[rtx], gfp);
127 	if (!sto)
128 		return NULL;
129 
130 	sx = calc_sx(dma_addr);
131 	pto = dma_get_page_table_origin(&sto[sx], gfp);
132 	if (!pto)
133 		return NULL;
134 
135 	px = calc_px(dma_addr);
136 	return &pto[px];
137 }
138 
139 void dma_update_cpu_trans(unsigned long *ptep, phys_addr_t page_addr, int flags)
140 {
141 	unsigned long pte;
142 
143 	pte = READ_ONCE(*ptep);
144 	if (flags & ZPCI_PTE_INVALID) {
145 		invalidate_pt_entry(&pte);
146 	} else {
147 		set_pt_pfaa(&pte, page_addr);
148 		validate_pt_entry(&pte);
149 	}
150 
151 	if (flags & ZPCI_TABLE_PROTECTED)
152 		entry_set_protected(&pte);
153 	else
154 		entry_clr_protected(&pte);
155 
156 	xchg(ptep, pte);
157 }
158 
159 static int __dma_update_trans(struct zpci_dev *zdev, phys_addr_t pa,
160 			      dma_addr_t dma_addr, size_t size, int flags)
161 {
162 	unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
163 	phys_addr_t page_addr = (pa & PAGE_MASK);
164 	unsigned long *entry;
165 	int i, rc = 0;
166 
167 	if (!nr_pages)
168 		return -EINVAL;
169 
170 	if (!zdev->dma_table)
171 		return -EINVAL;
172 
173 	for (i = 0; i < nr_pages; i++) {
174 		entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr,
175 					   GFP_ATOMIC);
176 		if (!entry) {
177 			rc = -ENOMEM;
178 			goto undo_cpu_trans;
179 		}
180 		dma_update_cpu_trans(entry, page_addr, flags);
181 		page_addr += PAGE_SIZE;
182 		dma_addr += PAGE_SIZE;
183 	}
184 
185 undo_cpu_trans:
186 	if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
187 		flags = ZPCI_PTE_INVALID;
188 		while (i-- > 0) {
189 			page_addr -= PAGE_SIZE;
190 			dma_addr -= PAGE_SIZE;
191 			entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr,
192 						   GFP_ATOMIC);
193 			if (!entry)
194 				break;
195 			dma_update_cpu_trans(entry, page_addr, flags);
196 		}
197 	}
198 	return rc;
199 }
200 
201 static int __dma_purge_tlb(struct zpci_dev *zdev, dma_addr_t dma_addr,
202 			   size_t size, int flags)
203 {
204 	unsigned long irqflags;
205 	int ret;
206 
207 	/*
208 	 * With zdev->tlb_refresh == 0, rpcit is not required to establish new
209 	 * translations when previously invalid translation-table entries are
210 	 * validated. With lazy unmap, rpcit is skipped for previously valid
211 	 * entries, but a global rpcit is then required before any address can
212 	 * be re-used, i.e. after each iommu bitmap wrap-around.
213 	 */
214 	if ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID) {
215 		if (!zdev->tlb_refresh)
216 			return 0;
217 	} else {
218 		if (!s390_iommu_strict)
219 			return 0;
220 	}
221 
222 	ret = zpci_refresh_trans((u64) zdev->fh << 32, dma_addr,
223 				 PAGE_ALIGN(size));
224 	if (ret == -ENOMEM && !s390_iommu_strict) {
225 		/* enable the hypervisor to free some resources */
226 		if (zpci_refresh_global(zdev))
227 			goto out;
228 
229 		spin_lock_irqsave(&zdev->iommu_bitmap_lock, irqflags);
230 		bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap,
231 			      zdev->lazy_bitmap, zdev->iommu_pages);
232 		bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages);
233 		spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, irqflags);
234 		ret = 0;
235 	}
236 out:
237 	return ret;
238 }
239 
240 static int dma_update_trans(struct zpci_dev *zdev, phys_addr_t pa,
241 			    dma_addr_t dma_addr, size_t size, int flags)
242 {
243 	int rc;
244 
245 	rc = __dma_update_trans(zdev, pa, dma_addr, size, flags);
246 	if (rc)
247 		return rc;
248 
249 	rc = __dma_purge_tlb(zdev, dma_addr, size, flags);
250 	if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID))
251 		__dma_update_trans(zdev, pa, dma_addr, size, ZPCI_PTE_INVALID);
252 
253 	return rc;
254 }
255 
256 void dma_free_seg_table(unsigned long entry)
257 {
258 	unsigned long *sto = get_rt_sto(entry);
259 	int sx;
260 
261 	for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
262 		if (reg_entry_isvalid(sto[sx]))
263 			dma_free_page_table(get_st_pto(sto[sx]));
264 
265 	dma_free_cpu_table(sto);
266 }
267 
268 void dma_cleanup_tables(unsigned long *table)
269 {
270 	int rtx;
271 
272 	if (!table)
273 		return;
274 
275 	for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
276 		if (reg_entry_isvalid(table[rtx]))
277 			dma_free_seg_table(table[rtx]);
278 
279 	dma_free_cpu_table(table);
280 }
281 
282 static unsigned long __dma_alloc_iommu(struct device *dev,
283 				       unsigned long start, int size)
284 {
285 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
286 
287 	return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
288 				start, size, zdev->start_dma >> PAGE_SHIFT,
289 				dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT),
290 				0);
291 }
292 
293 static dma_addr_t dma_alloc_address(struct device *dev, int size)
294 {
295 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
296 	unsigned long offset, flags;
297 
298 	spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
299 	offset = __dma_alloc_iommu(dev, zdev->next_bit, size);
300 	if (offset == -1) {
301 		if (!s390_iommu_strict) {
302 			/* global flush before DMA addresses are reused */
303 			if (zpci_refresh_global(zdev))
304 				goto out_error;
305 
306 			bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap,
307 				      zdev->lazy_bitmap, zdev->iommu_pages);
308 			bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages);
309 		}
310 		/* wrap-around */
311 		offset = __dma_alloc_iommu(dev, 0, size);
312 		if (offset == -1)
313 			goto out_error;
314 	}
315 	zdev->next_bit = offset + size;
316 	spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
317 
318 	return zdev->start_dma + offset * PAGE_SIZE;
319 
320 out_error:
321 	spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
322 	return DMA_MAPPING_ERROR;
323 }
324 
325 static void dma_free_address(struct device *dev, dma_addr_t dma_addr, int size)
326 {
327 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
328 	unsigned long flags, offset;
329 
330 	offset = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;
331 
332 	spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
333 	if (!zdev->iommu_bitmap)
334 		goto out;
335 
336 	if (s390_iommu_strict)
337 		bitmap_clear(zdev->iommu_bitmap, offset, size);
338 	else
339 		bitmap_set(zdev->lazy_bitmap, offset, size);
340 
341 out:
342 	spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
343 }
344 
345 static inline void zpci_err_dma(unsigned long rc, unsigned long addr)
346 {
347 	struct {
348 		unsigned long rc;
349 		unsigned long addr;
350 	} __packed data = {rc, addr};
351 
352 	zpci_err_hex(&data, sizeof(data));
353 }
354 
355 static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,
356 				     unsigned long offset, size_t size,
357 				     enum dma_data_direction direction,
358 				     unsigned long attrs)
359 {
360 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
361 	unsigned long pa = page_to_phys(page) + offset;
362 	int flags = ZPCI_PTE_VALID;
363 	unsigned long nr_pages;
364 	dma_addr_t dma_addr;
365 	int ret;
366 
367 	/* This rounds up number of pages based on size and offset */
368 	nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);
369 	dma_addr = dma_alloc_address(dev, nr_pages);
370 	if (dma_addr == DMA_MAPPING_ERROR) {
371 		ret = -ENOSPC;
372 		goto out_err;
373 	}
374 
375 	/* Use rounded up size */
376 	size = nr_pages * PAGE_SIZE;
377 
378 	if (direction == DMA_NONE || direction == DMA_TO_DEVICE)
379 		flags |= ZPCI_TABLE_PROTECTED;
380 
381 	ret = dma_update_trans(zdev, pa, dma_addr, size, flags);
382 	if (ret)
383 		goto out_free;
384 
385 	atomic64_add(nr_pages, &zdev->mapped_pages);
386 	return dma_addr + (offset & ~PAGE_MASK);
387 
388 out_free:
389 	dma_free_address(dev, dma_addr, nr_pages);
390 out_err:
391 	zpci_err("map error:\n");
392 	zpci_err_dma(ret, pa);
393 	return DMA_MAPPING_ERROR;
394 }
395 
396 static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,
397 				 size_t size, enum dma_data_direction direction,
398 				 unsigned long attrs)
399 {
400 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
401 	int npages, ret;
402 
403 	npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
404 	dma_addr = dma_addr & PAGE_MASK;
405 	ret = dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,
406 			       ZPCI_PTE_INVALID);
407 	if (ret) {
408 		zpci_err("unmap error:\n");
409 		zpci_err_dma(ret, dma_addr);
410 		return;
411 	}
412 
413 	atomic64_add(npages, &zdev->unmapped_pages);
414 	dma_free_address(dev, dma_addr, npages);
415 }
416 
417 static void *s390_dma_alloc(struct device *dev, size_t size,
418 			    dma_addr_t *dma_handle, gfp_t flag,
419 			    unsigned long attrs)
420 {
421 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
422 	struct page *page;
423 	phys_addr_t pa;
424 	dma_addr_t map;
425 
426 	size = PAGE_ALIGN(size);
427 	page = alloc_pages(flag | __GFP_ZERO, get_order(size));
428 	if (!page)
429 		return NULL;
430 
431 	pa = page_to_phys(page);
432 	map = s390_dma_map_pages(dev, page, 0, size, DMA_BIDIRECTIONAL, 0);
433 	if (dma_mapping_error(dev, map)) {
434 		__free_pages(page, get_order(size));
435 		return NULL;
436 	}
437 
438 	atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages);
439 	if (dma_handle)
440 		*dma_handle = map;
441 	return phys_to_virt(pa);
442 }
443 
444 static void s390_dma_free(struct device *dev, size_t size,
445 			  void *vaddr, dma_addr_t dma_handle,
446 			  unsigned long attrs)
447 {
448 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
449 
450 	size = PAGE_ALIGN(size);
451 	atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages);
452 	s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, 0);
453 	free_pages((unsigned long)vaddr, get_order(size));
454 }
455 
456 /* Map a segment into a contiguous dma address area */
457 static int __s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
458 			     size_t size, dma_addr_t *handle,
459 			     enum dma_data_direction dir)
460 {
461 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
462 	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
463 	dma_addr_t dma_addr_base, dma_addr;
464 	int flags = ZPCI_PTE_VALID;
465 	struct scatterlist *s;
466 	phys_addr_t pa = 0;
467 	int ret;
468 
469 	dma_addr_base = dma_alloc_address(dev, nr_pages);
470 	if (dma_addr_base == DMA_MAPPING_ERROR)
471 		return -ENOMEM;
472 
473 	dma_addr = dma_addr_base;
474 	if (dir == DMA_NONE || dir == DMA_TO_DEVICE)
475 		flags |= ZPCI_TABLE_PROTECTED;
476 
477 	for (s = sg; dma_addr < dma_addr_base + size; s = sg_next(s)) {
478 		pa = page_to_phys(sg_page(s));
479 		ret = __dma_update_trans(zdev, pa, dma_addr,
480 					 s->offset + s->length, flags);
481 		if (ret)
482 			goto unmap;
483 
484 		dma_addr += s->offset + s->length;
485 	}
486 	ret = __dma_purge_tlb(zdev, dma_addr_base, size, flags);
487 	if (ret)
488 		goto unmap;
489 
490 	*handle = dma_addr_base;
491 	atomic64_add(nr_pages, &zdev->mapped_pages);
492 
493 	return ret;
494 
495 unmap:
496 	dma_update_trans(zdev, 0, dma_addr_base, dma_addr - dma_addr_base,
497 			 ZPCI_PTE_INVALID);
498 	dma_free_address(dev, dma_addr_base, nr_pages);
499 	zpci_err("map error:\n");
500 	zpci_err_dma(ret, pa);
501 	return ret;
502 }
503 
504 static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
505 			   int nr_elements, enum dma_data_direction dir,
506 			   unsigned long attrs)
507 {
508 	struct scatterlist *s = sg, *start = sg, *dma = sg;
509 	unsigned int max = dma_get_max_seg_size(dev);
510 	unsigned int size = s->offset + s->length;
511 	unsigned int offset = s->offset;
512 	int count = 0, i, ret;
513 
514 	for (i = 1; i < nr_elements; i++) {
515 		s = sg_next(s);
516 
517 		s->dma_length = 0;
518 
519 		if (s->offset || (size & ~PAGE_MASK) ||
520 		    size + s->length > max) {
521 			ret = __s390_dma_map_sg(dev, start, size,
522 						&dma->dma_address, dir);
523 			if (ret)
524 				goto unmap;
525 
526 			dma->dma_address += offset;
527 			dma->dma_length = size - offset;
528 
529 			size = offset = s->offset;
530 			start = s;
531 			dma = sg_next(dma);
532 			count++;
533 		}
534 		size += s->length;
535 	}
536 	ret = __s390_dma_map_sg(dev, start, size, &dma->dma_address, dir);
537 	if (ret)
538 		goto unmap;
539 
540 	dma->dma_address += offset;
541 	dma->dma_length = size - offset;
542 
543 	return count + 1;
544 unmap:
545 	for_each_sg(sg, s, count, i)
546 		s390_dma_unmap_pages(dev, sg_dma_address(s), sg_dma_len(s),
547 				     dir, attrs);
548 
549 	return ret;
550 }
551 
552 static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
553 			      int nr_elements, enum dma_data_direction dir,
554 			      unsigned long attrs)
555 {
556 	struct scatterlist *s;
557 	int i;
558 
559 	for_each_sg(sg, s, nr_elements, i) {
560 		if (s->dma_length)
561 			s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,
562 					     dir, attrs);
563 		s->dma_address = 0;
564 		s->dma_length = 0;
565 	}
566 }
567 
568 int zpci_dma_init_device(struct zpci_dev *zdev)
569 {
570 	u8 status;
571 	int rc;
572 
573 	/*
574 	 * At this point, if the device is part of an IOMMU domain, this would
575 	 * be a strong hint towards a bug in the IOMMU API (common) code and/or
576 	 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
577 	 */
578 	WARN_ON(zdev->s390_domain);
579 
580 	spin_lock_init(&zdev->iommu_bitmap_lock);
581 
582 	zdev->dma_table = dma_alloc_cpu_table(GFP_KERNEL);
583 	if (!zdev->dma_table) {
584 		rc = -ENOMEM;
585 		goto out;
586 	}
587 
588 	/*
589 	 * Restrict the iommu bitmap size to the minimum of the following:
590 	 * - s390_iommu_aperture which defaults to high_memory
591 	 * - 3-level pagetable address limit minus start_dma offset
592 	 * - DMA address range allowed by the hardware (clp query pci fn)
593 	 *
594 	 * Also set zdev->end_dma to the actual end address of the usable
595 	 * range, instead of the theoretical maximum as reported by hardware.
596 	 *
597 	 * This limits the number of concurrently usable DMA mappings since
598 	 * for each DMA mapped memory address we need a DMA address including
599 	 * extra DMA addresses for multiple mappings of the same memory address.
600 	 */
601 	zdev->start_dma = PAGE_ALIGN(zdev->start_dma);
602 	zdev->iommu_size = min3(s390_iommu_aperture,
603 				ZPCI_TABLE_SIZE_RT - zdev->start_dma,
604 				zdev->end_dma - zdev->start_dma + 1);
605 	zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1;
606 	zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
607 	zdev->iommu_bitmap = vzalloc(zdev->iommu_pages / 8);
608 	if (!zdev->iommu_bitmap) {
609 		rc = -ENOMEM;
610 		goto free_dma_table;
611 	}
612 	if (!s390_iommu_strict) {
613 		zdev->lazy_bitmap = vzalloc(zdev->iommu_pages / 8);
614 		if (!zdev->lazy_bitmap) {
615 			rc = -ENOMEM;
616 			goto free_bitmap;
617 		}
618 
619 	}
620 	if (zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
621 			       virt_to_phys(zdev->dma_table), &status)) {
622 		rc = -EIO;
623 		goto free_bitmap;
624 	}
625 
626 	return 0;
627 free_bitmap:
628 	vfree(zdev->iommu_bitmap);
629 	zdev->iommu_bitmap = NULL;
630 	vfree(zdev->lazy_bitmap);
631 	zdev->lazy_bitmap = NULL;
632 free_dma_table:
633 	dma_free_cpu_table(zdev->dma_table);
634 	zdev->dma_table = NULL;
635 out:
636 	return rc;
637 }
638 
639 int zpci_dma_exit_device(struct zpci_dev *zdev)
640 {
641 	int cc = 0;
642 
643 	/*
644 	 * At this point, if the device is part of an IOMMU domain, this would
645 	 * be a strong hint towards a bug in the IOMMU API (common) code and/or
646 	 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
647 	 */
648 	WARN_ON(zdev->s390_domain);
649 	if (zdev_enabled(zdev))
650 		cc = zpci_unregister_ioat(zdev, 0);
651 	/*
652 	 * cc == 3 indicates the function is gone already. This can happen
653 	 * if the function was deconfigured/disabled suddenly and we have not
654 	 * received a new handle yet.
655 	 */
656 	if (cc && cc != 3)
657 		return -EIO;
658 
659 	dma_cleanup_tables(zdev->dma_table);
660 	zdev->dma_table = NULL;
661 	vfree(zdev->iommu_bitmap);
662 	zdev->iommu_bitmap = NULL;
663 	vfree(zdev->lazy_bitmap);
664 	zdev->lazy_bitmap = NULL;
665 	zdev->next_bit = 0;
666 	return 0;
667 }
668 
669 static int __init dma_alloc_cpu_table_caches(void)
670 {
671 	dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
672 					ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,
673 					0, NULL);
674 	if (!dma_region_table_cache)
675 		return -ENOMEM;
676 
677 	dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
678 					ZPCI_PT_SIZE, ZPCI_PT_ALIGN,
679 					0, NULL);
680 	if (!dma_page_table_cache) {
681 		kmem_cache_destroy(dma_region_table_cache);
682 		return -ENOMEM;
683 	}
684 	return 0;
685 }
686 
687 int __init zpci_dma_init(void)
688 {
689 	s390_iommu_aperture = (u64)virt_to_phys(high_memory);
690 	if (!s390_iommu_aperture_factor)
691 		s390_iommu_aperture = ULONG_MAX;
692 	else
693 		s390_iommu_aperture *= s390_iommu_aperture_factor;
694 
695 	return dma_alloc_cpu_table_caches();
696 }
697 
698 void zpci_dma_exit(void)
699 {
700 	kmem_cache_destroy(dma_page_table_cache);
701 	kmem_cache_destroy(dma_region_table_cache);
702 }
703 
704 const struct dma_map_ops s390_pci_dma_ops = {
705 	.alloc		= s390_dma_alloc,
706 	.free		= s390_dma_free,
707 	.map_sg		= s390_dma_map_sg,
708 	.unmap_sg	= s390_dma_unmap_sg,
709 	.map_page	= s390_dma_map_pages,
710 	.unmap_page	= s390_dma_unmap_pages,
711 	.mmap		= dma_common_mmap,
712 	.get_sgtable	= dma_common_get_sgtable,
713 	.alloc_pages	= dma_common_alloc_pages,
714 	.free_pages	= dma_common_free_pages,
715 	/* dma_supported is unconditionally true without a callback */
716 };
717 EXPORT_SYMBOL_GPL(s390_pci_dma_ops);
718 
719 static int __init s390_iommu_setup(char *str)
720 {
721 	if (!strcmp(str, "strict"))
722 		s390_iommu_strict = 1;
723 	return 1;
724 }
725 
726 __setup("s390_iommu=", s390_iommu_setup);
727 
728 static int __init s390_iommu_aperture_setup(char *str)
729 {
730 	if (kstrtou32(str, 10, &s390_iommu_aperture_factor))
731 		s390_iommu_aperture_factor = 1;
732 	return 1;
733 }
734 
735 __setup("s390_iommu_aperture=", s390_iommu_aperture_setup);
736