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