iommu/s390: Add iommu api for s390 pci devices
[cascardo/linux.git] / arch / s390 / pci / pci_dma.c
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>
13 #include <linux/vmalloc.h>
14 #include <linux/pci.h>
15 #include <asm/pci_dma.h>
16
17 static struct kmem_cache *dma_region_table_cache;
18 static struct kmem_cache *dma_page_table_cache;
19 static int s390_iommu_strict;
20
21 static 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 }
26
27 unsigned long *dma_alloc_cpu_table(void)
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++)
36                 *entry = ZPCI_TABLE_INVALID | ZPCI_TABLE_PROTECTED;
37         return table;
38 }
39
40 static void dma_free_cpu_table(void *table)
41 {
42         kmem_cache_free(dma_region_table_cache, table);
43 }
44
45 static 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++)
54                 *entry = ZPCI_PTE_INVALID | ZPCI_TABLE_PROTECTED;
55         return table;
56 }
57
58 static void dma_free_page_table(void *table)
59 {
60         kmem_cache_free(dma_page_table_cache, table);
61 }
62
63 static 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
81 static 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
98 static unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr)
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
117 void dma_update_cpu_trans(unsigned long *dma_table, void *page_addr,
118                           dma_addr_t dma_addr, int flags)
119 {
120         unsigned long *entry;
121
122         entry = dma_walk_cpu_trans(dma_table, dma_addr);
123         if (!entry) {
124                 WARN_ON_ONCE(1);
125                 return;
126         }
127
128         if (flags & ZPCI_PTE_INVALID) {
129                 invalidate_pt_entry(entry);
130                 return;
131         } else {
132                 set_pt_pfaa(entry, page_addr);
133                 validate_pt_entry(entry);
134         }
135
136         if (flags & ZPCI_TABLE_PROTECTED)
137                 entry_set_protected(entry);
138         else
139                 entry_clr_protected(entry);
140 }
141
142 static int dma_update_trans(struct zpci_dev *zdev, unsigned long pa,
143                             dma_addr_t dma_addr, size_t size, int flags)
144 {
145         unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
146         u8 *page_addr = (u8 *) (pa & PAGE_MASK);
147         dma_addr_t start_dma_addr = dma_addr;
148         unsigned long irq_flags;
149         int i, rc = 0;
150
151         if (!nr_pages)
152                 return -EINVAL;
153
154         spin_lock_irqsave(&zdev->dma_table_lock, irq_flags);
155         if (!zdev->dma_table)
156                 goto no_refresh;
157
158         for (i = 0; i < nr_pages; i++) {
159                 dma_update_cpu_trans(zdev->dma_table, page_addr, dma_addr,
160                                      flags);
161                 page_addr += PAGE_SIZE;
162                 dma_addr += PAGE_SIZE;
163         }
164
165         /*
166          * With zdev->tlb_refresh == 0, rpcit is not required to establish new
167          * translations when previously invalid translation-table entries are
168          * validated. With lazy unmap, it also is skipped for previously valid
169          * entries, but a global rpcit is then required before any address can
170          * be re-used, i.e. after each iommu bitmap wrap-around.
171          */
172         if (!zdev->tlb_refresh &&
173                         (!s390_iommu_strict ||
174                         ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)))
175                 goto no_refresh;
176
177         rc = zpci_refresh_trans((u64) zdev->fh << 32, start_dma_addr,
178                                 nr_pages * PAGE_SIZE);
179
180 no_refresh:
181         spin_unlock_irqrestore(&zdev->dma_table_lock, irq_flags);
182         return rc;
183 }
184
185 void dma_free_seg_table(unsigned long entry)
186 {
187         unsigned long *sto = get_rt_sto(entry);
188         int sx;
189
190         for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
191                 if (reg_entry_isvalid(sto[sx]))
192                         dma_free_page_table(get_st_pto(sto[sx]));
193
194         dma_free_cpu_table(sto);
195 }
196
197 void dma_cleanup_tables(unsigned long *table)
198 {
199         int rtx;
200
201         if (!table)
202                 return;
203
204         for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
205                 if (reg_entry_isvalid(table[rtx]))
206                         dma_free_seg_table(table[rtx]);
207
208         dma_free_cpu_table(table);
209 }
210
211 static unsigned long __dma_alloc_iommu(struct zpci_dev *zdev,
212                                        unsigned long start, int size)
213 {
214         unsigned long boundary_size;
215
216         boundary_size = ALIGN(dma_get_seg_boundary(&zdev->pdev->dev) + 1,
217                               PAGE_SIZE) >> PAGE_SHIFT;
218         return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
219                                 start, size, 0, boundary_size, 0);
220 }
221
222 static unsigned long dma_alloc_iommu(struct zpci_dev *zdev, int size)
223 {
224         unsigned long offset, flags;
225         int wrap = 0;
226
227         spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
228         offset = __dma_alloc_iommu(zdev, zdev->next_bit, size);
229         if (offset == -1) {
230                 /* wrap-around */
231                 offset = __dma_alloc_iommu(zdev, 0, size);
232                 wrap = 1;
233         }
234
235         if (offset != -1) {
236                 zdev->next_bit = offset + size;
237                 if (!zdev->tlb_refresh && !s390_iommu_strict && wrap)
238                         /* global flush after wrap-around with lazy unmap */
239                         zpci_refresh_global(zdev);
240         }
241         spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
242         return offset;
243 }
244
245 static void dma_free_iommu(struct zpci_dev *zdev, unsigned long offset, int size)
246 {
247         unsigned long flags;
248
249         spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
250         if (!zdev->iommu_bitmap)
251                 goto out;
252         bitmap_clear(zdev->iommu_bitmap, offset, size);
253         /*
254          * Lazy flush for unmap: need to move next_bit to avoid address re-use
255          * until wrap-around.
256          */
257         if (!s390_iommu_strict && offset >= zdev->next_bit)
258                 zdev->next_bit = offset + size;
259 out:
260         spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
261 }
262
263 static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,
264                                      unsigned long offset, size_t size,
265                                      enum dma_data_direction direction,
266                                      struct dma_attrs *attrs)
267 {
268         struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
269         unsigned long nr_pages, iommu_page_index;
270         unsigned long pa = page_to_phys(page) + offset;
271         int flags = ZPCI_PTE_VALID;
272         dma_addr_t dma_addr;
273
274         /* This rounds up number of pages based on size and offset */
275         nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);
276         iommu_page_index = dma_alloc_iommu(zdev, nr_pages);
277         if (iommu_page_index == -1)
278                 goto out_err;
279
280         /* Use rounded up size */
281         size = nr_pages * PAGE_SIZE;
282
283         dma_addr = zdev->start_dma + iommu_page_index * PAGE_SIZE;
284         if (dma_addr + size > zdev->end_dma)
285                 goto out_free;
286
287         if (direction == DMA_NONE || direction == DMA_TO_DEVICE)
288                 flags |= ZPCI_TABLE_PROTECTED;
289
290         if (!dma_update_trans(zdev, pa, dma_addr, size, flags)) {
291                 atomic64_add(nr_pages, &zdev->mapped_pages);
292                 return dma_addr + (offset & ~PAGE_MASK);
293         }
294
295 out_free:
296         dma_free_iommu(zdev, iommu_page_index, nr_pages);
297 out_err:
298         zpci_err("map error:\n");
299         zpci_err_hex(&pa, sizeof(pa));
300         return DMA_ERROR_CODE;
301 }
302
303 static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,
304                                  size_t size, enum dma_data_direction direction,
305                                  struct dma_attrs *attrs)
306 {
307         struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
308         unsigned long iommu_page_index;
309         int npages;
310
311         npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
312         dma_addr = dma_addr & PAGE_MASK;
313         if (dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,
314                              ZPCI_TABLE_PROTECTED | ZPCI_PTE_INVALID)) {
315                 zpci_err("unmap error:\n");
316                 zpci_err_hex(&dma_addr, sizeof(dma_addr));
317         }
318
319         atomic64_add(npages, &zdev->unmapped_pages);
320         iommu_page_index = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;
321         dma_free_iommu(zdev, iommu_page_index, npages);
322 }
323
324 static void *s390_dma_alloc(struct device *dev, size_t size,
325                             dma_addr_t *dma_handle, gfp_t flag,
326                             struct dma_attrs *attrs)
327 {
328         struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
329         struct page *page;
330         unsigned long pa;
331         dma_addr_t map;
332
333         size = PAGE_ALIGN(size);
334         page = alloc_pages(flag, get_order(size));
335         if (!page)
336                 return NULL;
337
338         pa = page_to_phys(page);
339         memset((void *) pa, 0, size);
340
341         map = s390_dma_map_pages(dev, page, pa % PAGE_SIZE,
342                                  size, DMA_BIDIRECTIONAL, NULL);
343         if (dma_mapping_error(dev, map)) {
344                 free_pages(pa, get_order(size));
345                 return NULL;
346         }
347
348         atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages);
349         if (dma_handle)
350                 *dma_handle = map;
351         return (void *) pa;
352 }
353
354 static void s390_dma_free(struct device *dev, size_t size,
355                           void *pa, dma_addr_t dma_handle,
356                           struct dma_attrs *attrs)
357 {
358         struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
359
360         size = PAGE_ALIGN(size);
361         atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages);
362         s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, NULL);
363         free_pages((unsigned long) pa, get_order(size));
364 }
365
366 static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
367                            int nr_elements, enum dma_data_direction dir,
368                            struct dma_attrs *attrs)
369 {
370         int mapped_elements = 0;
371         struct scatterlist *s;
372         int i;
373
374         for_each_sg(sg, s, nr_elements, i) {
375                 struct page *page = sg_page(s);
376                 s->dma_address = s390_dma_map_pages(dev, page, s->offset,
377                                                     s->length, dir, NULL);
378                 if (!dma_mapping_error(dev, s->dma_address)) {
379                         s->dma_length = s->length;
380                         mapped_elements++;
381                 } else
382                         goto unmap;
383         }
384 out:
385         return mapped_elements;
386
387 unmap:
388         for_each_sg(sg, s, mapped_elements, i) {
389                 if (s->dma_address)
390                         s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,
391                                              dir, NULL);
392                 s->dma_address = 0;
393                 s->dma_length = 0;
394         }
395         mapped_elements = 0;
396         goto out;
397 }
398
399 static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
400                               int nr_elements, enum dma_data_direction dir,
401                               struct dma_attrs *attrs)
402 {
403         struct scatterlist *s;
404         int i;
405
406         for_each_sg(sg, s, nr_elements, i) {
407                 s390_dma_unmap_pages(dev, s->dma_address, s->dma_length, dir, NULL);
408                 s->dma_address = 0;
409                 s->dma_length = 0;
410         }
411 }
412
413 int zpci_dma_init_device(struct zpci_dev *zdev)
414 {
415         int rc;
416
417         /*
418          * At this point, if the device is part of an IOMMU domain, this would
419          * be a strong hint towards a bug in the IOMMU API (common) code and/or
420          * simultaneous access via IOMMU and DMA API. So let's issue a warning.
421          */
422         WARN_ON(zdev->s390_domain);
423
424         spin_lock_init(&zdev->iommu_bitmap_lock);
425         spin_lock_init(&zdev->dma_table_lock);
426
427         zdev->dma_table = dma_alloc_cpu_table();
428         if (!zdev->dma_table) {
429                 rc = -ENOMEM;
430                 goto out_clean;
431         }
432
433         zdev->iommu_size = (unsigned long) high_memory - PAGE_OFFSET;
434         zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
435         zdev->iommu_bitmap = vzalloc(zdev->iommu_pages / 8);
436         if (!zdev->iommu_bitmap) {
437                 rc = -ENOMEM;
438                 goto out_reg;
439         }
440
441         rc = zpci_register_ioat(zdev,
442                                 0,
443                                 zdev->start_dma + PAGE_OFFSET,
444                                 zdev->start_dma + zdev->iommu_size - 1,
445                                 (u64) zdev->dma_table);
446         if (rc)
447                 goto out_reg;
448         return 0;
449
450 out_reg:
451         dma_free_cpu_table(zdev->dma_table);
452 out_clean:
453         return rc;
454 }
455
456 void zpci_dma_exit_device(struct zpci_dev *zdev)
457 {
458         /*
459          * At this point, if the device is part of an IOMMU domain, this would
460          * be a strong hint towards a bug in the IOMMU API (common) code and/or
461          * simultaneous access via IOMMU and DMA API. So let's issue a warning.
462          */
463         WARN_ON(zdev->s390_domain);
464
465         zpci_unregister_ioat(zdev, 0);
466         dma_cleanup_tables(zdev->dma_table);
467         zdev->dma_table = NULL;
468         vfree(zdev->iommu_bitmap);
469         zdev->iommu_bitmap = NULL;
470         zdev->next_bit = 0;
471 }
472
473 static int __init dma_alloc_cpu_table_caches(void)
474 {
475         dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
476                                         ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,
477                                         0, NULL);
478         if (!dma_region_table_cache)
479                 return -ENOMEM;
480
481         dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
482                                         ZPCI_PT_SIZE, ZPCI_PT_ALIGN,
483                                         0, NULL);
484         if (!dma_page_table_cache) {
485                 kmem_cache_destroy(dma_region_table_cache);
486                 return -ENOMEM;
487         }
488         return 0;
489 }
490
491 int __init zpci_dma_init(void)
492 {
493         return dma_alloc_cpu_table_caches();
494 }
495
496 void zpci_dma_exit(void)
497 {
498         kmem_cache_destroy(dma_page_table_cache);
499         kmem_cache_destroy(dma_region_table_cache);
500 }
501
502 #define PREALLOC_DMA_DEBUG_ENTRIES      (1 << 16)
503
504 static int __init dma_debug_do_init(void)
505 {
506         dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
507         return 0;
508 }
509 fs_initcall(dma_debug_do_init);
510
511 struct dma_map_ops s390_dma_ops = {
512         .alloc          = s390_dma_alloc,
513         .free           = s390_dma_free,
514         .map_sg         = s390_dma_map_sg,
515         .unmap_sg       = s390_dma_unmap_sg,
516         .map_page       = s390_dma_map_pages,
517         .unmap_page     = s390_dma_unmap_pages,
518         /* if we support direct DMA this must be conditional */
519         .is_phys        = 0,
520         /* dma_supported is unconditionally true without a callback */
521 };
522 EXPORT_SYMBOL_GPL(s390_dma_ops);
523
524 static int __init s390_iommu_setup(char *str)
525 {
526         if (!strncmp(str, "strict", 6))
527                 s390_iommu_strict = 1;
528         return 0;
529 }
530
531 __setup("s390_iommu=", s390_iommu_setup);