~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/s390/pci/pci_dma.c

Version: ~ [ linux-5.5-rc1 ] ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp