]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/s390/pci/pci_dma.c
dma-mapping: use unsigned long for dma_attrs
[mirror_ubuntu-artful-kernel.git] / arch / s390 / pci / pci_dma.c
CommitLineData
828b35f6
JG
1/*
2 * Copyright IBM Corp. 2012
3 *
4 * Author(s):
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 */
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include <linux/export.h>
11#include <linux/iommu-helper.h>
12#include <linux/dma-mapping.h>
22459321 13#include <linux/vmalloc.h>
828b35f6
JG
14#include <linux/pci.h>
15#include <asm/pci_dma.h>
16
828b35f6
JG
17static struct kmem_cache *dma_region_table_cache;
18static struct kmem_cache *dma_page_table_cache;
c60d1ae4
GS
19static int s390_iommu_strict;
20
21static int zpci_refresh_global(struct zpci_dev *zdev)
22{
23 return zpci_refresh_trans((u64) zdev->fh << 32, zdev->start_dma,
24 zdev->iommu_pages * PAGE_SIZE);
25}
828b35f6 26
8128f23c 27unsigned long *dma_alloc_cpu_table(void)
828b35f6
JG
28{
29 unsigned long *table, *entry;
30
31 table = kmem_cache_alloc(dma_region_table_cache, GFP_ATOMIC);
32 if (!table)
33 return NULL;
34
35 for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
4d5a6b72 36 *entry = ZPCI_TABLE_INVALID;
828b35f6
JG
37 return table;
38}
39
40static void dma_free_cpu_table(void *table)
41{
42 kmem_cache_free(dma_region_table_cache, table);
43}
44
45static unsigned long *dma_alloc_page_table(void)
46{
47 unsigned long *table, *entry;
48
49 table = kmem_cache_alloc(dma_page_table_cache, GFP_ATOMIC);
50 if (!table)
51 return NULL;
52
53 for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
4d5a6b72 54 *entry = ZPCI_PTE_INVALID;
828b35f6
JG
55 return table;
56}
57
58static void dma_free_page_table(void *table)
59{
60 kmem_cache_free(dma_page_table_cache, table);
61}
62
63static unsigned long *dma_get_seg_table_origin(unsigned long *entry)
64{
65 unsigned long *sto;
66
67 if (reg_entry_isvalid(*entry))
68 sto = get_rt_sto(*entry);
69 else {
70 sto = dma_alloc_cpu_table();
71 if (!sto)
72 return NULL;
73
74 set_rt_sto(entry, sto);
75 validate_rt_entry(entry);
76 entry_clr_protected(entry);
77 }
78 return sto;
79}
80
81static unsigned long *dma_get_page_table_origin(unsigned long *entry)
82{
83 unsigned long *pto;
84
85 if (reg_entry_isvalid(*entry))
86 pto = get_st_pto(*entry);
87 else {
88 pto = dma_alloc_page_table();
89 if (!pto)
90 return NULL;
91 set_st_pto(entry, pto);
92 validate_st_entry(entry);
93 entry_clr_protected(entry);
94 }
95 return pto;
96}
97
66728eee 98unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr)
828b35f6
JG
99{
100 unsigned long *sto, *pto;
101 unsigned int rtx, sx, px;
102
103 rtx = calc_rtx(dma_addr);
104 sto = dma_get_seg_table_origin(&rto[rtx]);
105 if (!sto)
106 return NULL;
107
108 sx = calc_sx(dma_addr);
109 pto = dma_get_page_table_origin(&sto[sx]);
110 if (!pto)
111 return NULL;
112
113 px = calc_px(dma_addr);
114 return &pto[px];
115}
116
66728eee 117void dma_update_cpu_trans(unsigned long *entry, void *page_addr, int flags)
828b35f6 118{
828b35f6
JG
119 if (flags & ZPCI_PTE_INVALID) {
120 invalidate_pt_entry(entry);
828b35f6
JG
121 } else {
122 set_pt_pfaa(entry, page_addr);
123 validate_pt_entry(entry);
124 }
125
126 if (flags & ZPCI_TABLE_PROTECTED)
127 entry_set_protected(entry);
128 else
129 entry_clr_protected(entry);
130}
131
132static int dma_update_trans(struct zpci_dev *zdev, unsigned long pa,
133 dma_addr_t dma_addr, size_t size, int flags)
134{
135 unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
136 u8 *page_addr = (u8 *) (pa & PAGE_MASK);
137 dma_addr_t start_dma_addr = dma_addr;
138 unsigned long irq_flags;
66728eee 139 unsigned long *entry;
828b35f6
JG
140 int i, rc = 0;
141
142 if (!nr_pages)
143 return -EINVAL;
144
145 spin_lock_irqsave(&zdev->dma_table_lock, irq_flags);
66728eee
SO
146 if (!zdev->dma_table) {
147 rc = -EINVAL;
828b35f6 148 goto no_refresh;
66728eee 149 }
828b35f6
JG
150
151 for (i = 0; i < nr_pages; i++) {
66728eee
SO
152 entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr);
153 if (!entry) {
154 rc = -ENOMEM;
155 goto undo_cpu_trans;
156 }
157 dma_update_cpu_trans(entry, page_addr, flags);
828b35f6
JG
158 page_addr += PAGE_SIZE;
159 dma_addr += PAGE_SIZE;
160 }
161
162 /*
c60d1ae4
GS
163 * With zdev->tlb_refresh == 0, rpcit is not required to establish new
164 * translations when previously invalid translation-table entries are
165 * validated. With lazy unmap, it also is skipped for previously valid
166 * entries, but a global rpcit is then required before any address can
167 * be re-used, i.e. after each iommu bitmap wrap-around.
828b35f6
JG
168 */
169 if (!zdev->tlb_refresh &&
c60d1ae4
GS
170 (!s390_iommu_strict ||
171 ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)))
828b35f6 172 goto no_refresh;
b2a9e87d 173
9389339f
MS
174 rc = zpci_refresh_trans((u64) zdev->fh << 32, start_dma_addr,
175 nr_pages * PAGE_SIZE);
66728eee
SO
176undo_cpu_trans:
177 if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
178 flags = ZPCI_PTE_INVALID;
179 while (i-- > 0) {
180 page_addr -= PAGE_SIZE;
181 dma_addr -= PAGE_SIZE;
182 entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr);
183 if (!entry)
184 break;
185 dma_update_cpu_trans(entry, page_addr, flags);
186 }
187 }
828b35f6
JG
188
189no_refresh:
190 spin_unlock_irqrestore(&zdev->dma_table_lock, irq_flags);
191 return rc;
192}
193
8128f23c 194void dma_free_seg_table(unsigned long entry)
828b35f6
JG
195{
196 unsigned long *sto = get_rt_sto(entry);
197 int sx;
198
199 for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
200 if (reg_entry_isvalid(sto[sx]))
201 dma_free_page_table(get_st_pto(sto[sx]));
202
203 dma_free_cpu_table(sto);
204}
205
8128f23c 206void dma_cleanup_tables(unsigned long *table)
828b35f6 207{
828b35f6
JG
208 int rtx;
209
8128f23c 210 if (!table)
828b35f6
JG
211 return;
212
213 for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
214 if (reg_entry_isvalid(table[rtx]))
215 dma_free_seg_table(table[rtx]);
216
217 dma_free_cpu_table(table);
828b35f6
JG
218}
219
9a99649f 220static unsigned long __dma_alloc_iommu(struct device *dev,
5ec6d491 221 unsigned long start, int size)
828b35f6 222{
9a99649f 223 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
5ec6d491 224 unsigned long boundary_size;
828b35f6 225
9a99649f 226 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
5ec6d491 227 PAGE_SIZE) >> PAGE_SHIFT;
828b35f6 228 return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
8ee2db3c
SO
229 start, size, zdev->start_dma >> PAGE_SHIFT,
230 boundary_size, 0);
828b35f6
JG
231}
232
9a99649f 233static unsigned long dma_alloc_iommu(struct device *dev, int size)
828b35f6 234{
9a99649f 235 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
828b35f6 236 unsigned long offset, flags;
c60d1ae4 237 int wrap = 0;
828b35f6
JG
238
239 spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
9a99649f 240 offset = __dma_alloc_iommu(dev, zdev->next_bit, size);
c60d1ae4
GS
241 if (offset == -1) {
242 /* wrap-around */
9a99649f 243 offset = __dma_alloc_iommu(dev, 0, size);
c60d1ae4
GS
244 wrap = 1;
245 }
828b35f6
JG
246
247 if (offset != -1) {
248 zdev->next_bit = offset + size;
c60d1ae4
GS
249 if (!zdev->tlb_refresh && !s390_iommu_strict && wrap)
250 /* global flush after wrap-around with lazy unmap */
251 zpci_refresh_global(zdev);
828b35f6
JG
252 }
253 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
254 return offset;
255}
256
9a99649f 257static void dma_free_iommu(struct device *dev, unsigned long offset, int size)
828b35f6 258{
9a99649f 259 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
828b35f6
JG
260 unsigned long flags;
261
262 spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
263 if (!zdev->iommu_bitmap)
264 goto out;
265 bitmap_clear(zdev->iommu_bitmap, offset, size);
c60d1ae4
GS
266 /*
267 * Lazy flush for unmap: need to move next_bit to avoid address re-use
268 * until wrap-around.
269 */
270 if (!s390_iommu_strict && offset >= zdev->next_bit)
828b35f6
JG
271 zdev->next_bit = offset + size;
272out:
273 spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
274}
275
52d43d81
SO
276static inline void zpci_err_dma(unsigned long rc, unsigned long addr)
277{
278 struct {
279 unsigned long rc;
280 unsigned long addr;
281 } __packed data = {rc, addr};
282
283 zpci_err_hex(&data, sizeof(data));
284}
285
828b35f6
JG
286static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,
287 unsigned long offset, size_t size,
288 enum dma_data_direction direction,
00085f1e 289 unsigned long attrs)
828b35f6 290{
198a5278 291 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
828b35f6
JG
292 unsigned long nr_pages, iommu_page_index;
293 unsigned long pa = page_to_phys(page) + offset;
294 int flags = ZPCI_PTE_VALID;
295 dma_addr_t dma_addr;
52d43d81 296 int ret;
828b35f6 297
828b35f6
JG
298 /* This rounds up number of pages based on size and offset */
299 nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);
9a99649f 300 iommu_page_index = dma_alloc_iommu(dev, nr_pages);
52d43d81
SO
301 if (iommu_page_index == -1) {
302 ret = -ENOSPC;
828b35f6 303 goto out_err;
52d43d81 304 }
828b35f6
JG
305
306 /* Use rounded up size */
307 size = nr_pages * PAGE_SIZE;
308
309 dma_addr = zdev->start_dma + iommu_page_index * PAGE_SIZE;
52d43d81
SO
310 if (dma_addr + size > zdev->end_dma) {
311 ret = -ERANGE;
828b35f6 312 goto out_free;
52d43d81 313 }
828b35f6
JG
314
315 if (direction == DMA_NONE || direction == DMA_TO_DEVICE)
316 flags |= ZPCI_TABLE_PROTECTED;
317
52d43d81
SO
318 ret = dma_update_trans(zdev, pa, dma_addr, size, flags);
319 if (ret)
320 goto out_free;
321
322 atomic64_add(nr_pages, &zdev->mapped_pages);
323 return dma_addr + (offset & ~PAGE_MASK);
828b35f6
JG
324
325out_free:
9a99649f 326 dma_free_iommu(dev, iommu_page_index, nr_pages);
828b35f6 327out_err:
1f1dcbd4 328 zpci_err("map error:\n");
52d43d81 329 zpci_err_dma(ret, pa);
828b35f6
JG
330 return DMA_ERROR_CODE;
331}
332
333static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,
334 size_t size, enum dma_data_direction direction,
00085f1e 335 unsigned long attrs)
828b35f6 336{
198a5278 337 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
828b35f6 338 unsigned long iommu_page_index;
52d43d81 339 int npages, ret;
828b35f6
JG
340
341 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
342 dma_addr = dma_addr & PAGE_MASK;
52d43d81
SO
343 ret = dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,
344 ZPCI_PTE_INVALID);
345 if (ret) {
1f1dcbd4 346 zpci_err("unmap error:\n");
52d43d81
SO
347 zpci_err_dma(ret, dma_addr);
348 return;
1f1dcbd4 349 }
828b35f6 350
6001018a 351 atomic64_add(npages, &zdev->unmapped_pages);
828b35f6 352 iommu_page_index = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;
9a99649f 353 dma_free_iommu(dev, iommu_page_index, npages);
828b35f6
JG
354}
355
356static void *s390_dma_alloc(struct device *dev, size_t size,
357 dma_addr_t *dma_handle, gfp_t flag,
00085f1e 358 unsigned long attrs)
828b35f6 359{
198a5278 360 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
828b35f6
JG
361 struct page *page;
362 unsigned long pa;
363 dma_addr_t map;
364
365 size = PAGE_ALIGN(size);
366 page = alloc_pages(flag, get_order(size));
367 if (!page)
368 return NULL;
d0b08853 369
828b35f6
JG
370 pa = page_to_phys(page);
371 memset((void *) pa, 0, size);
372
00085f1e 373 map = s390_dma_map_pages(dev, page, 0, size, DMA_BIDIRECTIONAL, 0);
828b35f6
JG
374 if (dma_mapping_error(dev, map)) {
375 free_pages(pa, get_order(size));
376 return NULL;
377 }
378
6001018a 379 atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages);
828b35f6
JG
380 if (dma_handle)
381 *dma_handle = map;
382 return (void *) pa;
383}
384
385static void s390_dma_free(struct device *dev, size_t size,
386 void *pa, dma_addr_t dma_handle,
00085f1e 387 unsigned long attrs)
828b35f6 388{
198a5278 389 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
f7038b7c
SO
390
391 size = PAGE_ALIGN(size);
6001018a 392 atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages);
00085f1e 393 s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, 0);
828b35f6
JG
394 free_pages((unsigned long) pa, get_order(size));
395}
396
397static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
398 int nr_elements, enum dma_data_direction dir,
00085f1e 399 unsigned long attrs)
828b35f6
JG
400{
401 int mapped_elements = 0;
402 struct scatterlist *s;
403 int i;
404
405 for_each_sg(sg, s, nr_elements, i) {
406 struct page *page = sg_page(s);
407 s->dma_address = s390_dma_map_pages(dev, page, s->offset,
00085f1e 408 s->length, dir, 0);
828b35f6
JG
409 if (!dma_mapping_error(dev, s->dma_address)) {
410 s->dma_length = s->length;
411 mapped_elements++;
412 } else
413 goto unmap;
414 }
415out:
416 return mapped_elements;
417
418unmap:
419 for_each_sg(sg, s, mapped_elements, i) {
420 if (s->dma_address)
421 s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,
00085f1e 422 dir, 0);
828b35f6
JG
423 s->dma_address = 0;
424 s->dma_length = 0;
425 }
426 mapped_elements = 0;
427 goto out;
428}
429
430static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
431 int nr_elements, enum dma_data_direction dir,
00085f1e 432 unsigned long attrs)
828b35f6
JG
433{
434 struct scatterlist *s;
435 int i;
436
437 for_each_sg(sg, s, nr_elements, i) {
00085f1e
KK
438 s390_dma_unmap_pages(dev, s->dma_address, s->dma_length, dir,
439 0);
828b35f6
JG
440 s->dma_address = 0;
441 s->dma_length = 0;
442 }
443}
444
445int zpci_dma_init_device(struct zpci_dev *zdev)
446{
828b35f6
JG
447 int rc;
448
8128f23c
GS
449 /*
450 * At this point, if the device is part of an IOMMU domain, this would
451 * be a strong hint towards a bug in the IOMMU API (common) code and/or
452 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
453 */
454 WARN_ON(zdev->s390_domain);
455
828b35f6
JG
456 spin_lock_init(&zdev->iommu_bitmap_lock);
457 spin_lock_init(&zdev->dma_table_lock);
458
459 zdev->dma_table = dma_alloc_cpu_table();
460 if (!zdev->dma_table) {
461 rc = -ENOMEM;
dba59909 462 goto out;
828b35f6
JG
463 }
464
69eea95c
GS
465 /*
466 * Restrict the iommu bitmap size to the minimum of the following:
467 * - main memory size
468 * - 3-level pagetable address limit minus start_dma offset
469 * - DMA address range allowed by the hardware (clp query pci fn)
470 *
471 * Also set zdev->end_dma to the actual end address of the usable
472 * range, instead of the theoretical maximum as reported by hardware.
473 */
53b1bc9a 474 zdev->start_dma = PAGE_ALIGN(zdev->start_dma);
69eea95c
GS
475 zdev->iommu_size = min3((u64) high_memory,
476 ZPCI_TABLE_SIZE_RT - zdev->start_dma,
477 zdev->end_dma - zdev->start_dma + 1);
478 zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1;
828b35f6 479 zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
22459321 480 zdev->iommu_bitmap = vzalloc(zdev->iommu_pages / 8);
828b35f6
JG
481 if (!zdev->iommu_bitmap) {
482 rc = -ENOMEM;
dba59909 483 goto free_dma_table;
828b35f6
JG
484 }
485
69eea95c 486 rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
828b35f6
JG
487 (u64) zdev->dma_table);
488 if (rc)
dba59909 489 goto free_bitmap;
828b35f6 490
dba59909
SO
491 return 0;
492free_bitmap:
493 vfree(zdev->iommu_bitmap);
494 zdev->iommu_bitmap = NULL;
495free_dma_table:
828b35f6 496 dma_free_cpu_table(zdev->dma_table);
dba59909
SO
497 zdev->dma_table = NULL;
498out:
828b35f6
JG
499 return rc;
500}
501
502void zpci_dma_exit_device(struct zpci_dev *zdev)
503{
8128f23c
GS
504 /*
505 * At this point, if the device is part of an IOMMU domain, this would
506 * be a strong hint towards a bug in the IOMMU API (common) code and/or
507 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
508 */
509 WARN_ON(zdev->s390_domain);
510
828b35f6 511 zpci_unregister_ioat(zdev, 0);
8128f23c
GS
512 dma_cleanup_tables(zdev->dma_table);
513 zdev->dma_table = NULL;
22459321 514 vfree(zdev->iommu_bitmap);
828b35f6
JG
515 zdev->iommu_bitmap = NULL;
516 zdev->next_bit = 0;
517}
518
519static int __init dma_alloc_cpu_table_caches(void)
520{
521 dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
522 ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,
523 0, NULL);
524 if (!dma_region_table_cache)
525 return -ENOMEM;
526
527 dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
528 ZPCI_PT_SIZE, ZPCI_PT_ALIGN,
529 0, NULL);
530 if (!dma_page_table_cache) {
531 kmem_cache_destroy(dma_region_table_cache);
532 return -ENOMEM;
533 }
534 return 0;
535}
536
537int __init zpci_dma_init(void)
538{
539 return dma_alloc_cpu_table_caches();
540}
541
542void zpci_dma_exit(void)
543{
544 kmem_cache_destroy(dma_page_table_cache);
545 kmem_cache_destroy(dma_region_table_cache);
546}
547
548#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
549
550static int __init dma_debug_do_init(void)
551{
552 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
553 return 0;
554}
555fs_initcall(dma_debug_do_init);
556
e82becfc 557struct dma_map_ops s390_pci_dma_ops = {
828b35f6
JG
558 .alloc = s390_dma_alloc,
559 .free = s390_dma_free,
560 .map_sg = s390_dma_map_sg,
561 .unmap_sg = s390_dma_unmap_sg,
562 .map_page = s390_dma_map_pages,
563 .unmap_page = s390_dma_unmap_pages,
564 /* if we support direct DMA this must be conditional */
565 .is_phys = 0,
566 /* dma_supported is unconditionally true without a callback */
567};
e82becfc 568EXPORT_SYMBOL_GPL(s390_pci_dma_ops);
c60d1ae4
GS
569
570static int __init s390_iommu_setup(char *str)
571{
572 if (!strncmp(str, "strict", 6))
573 s390_iommu_strict = 1;
574 return 0;
575}
576
577__setup("s390_iommu=", s390_iommu_setup);