Greg Kroah-Hartman | adbb390 | 2017-11-24 15:00:36 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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-mapping.h> |
Sebastian Ott | 2245932 | 2013-08-29 20:31:50 +0200 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 15 | #include <linux/pci.h> |
| 16 | #include <asm/pci_dma.h> |
| 17 | |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 18 | #define S390_MAPPING_ERROR (~(dma_addr_t) 0x0) |
| 19 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 20 | static struct kmem_cache *dma_region_table_cache; |
| 21 | static struct kmem_cache *dma_page_table_cache; |
Gerald Schaefer | c60d1ae | 2014-07-18 17:37:08 +0200 | [diff] [blame] | 22 | static int s390_iommu_strict; |
| 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 | } |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 29 | |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 30 | unsigned long *dma_alloc_cpu_table(void) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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++) |
Sebastian Ott | 4d5a6b7 | 2015-10-26 11:15:28 +0100 | [diff] [blame] | 39 | *entry = ZPCI_TABLE_INVALID; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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++) |
Sebastian Ott | 4d5a6b7 | 2015-10-26 11:15:28 +0100 | [diff] [blame] | 57 | *entry = ZPCI_PTE_INVALID; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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 | |
| 77 | set_rt_sto(entry, sto); |
| 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; |
| 94 | set_st_pto(entry, pto); |
| 95 | validate_st_entry(entry); |
| 96 | entry_clr_protected(entry); |
| 97 | } |
| 98 | return pto; |
| 99 | } |
| 100 | |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 101 | unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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 | |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 120 | void dma_update_cpu_trans(unsigned long *entry, void *page_addr, int flags) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 121 | { |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 122 | if (flags & ZPCI_PTE_INVALID) { |
| 123 | invalidate_pt_entry(entry); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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 | |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 135 | static int __dma_update_trans(struct zpci_dev *zdev, unsigned long pa, |
| 136 | dma_addr_t dma_addr, size_t size, int flags) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 137 | { |
| 138 | unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 139 | u8 *page_addr = (u8 *) (pa & PAGE_MASK); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 140 | unsigned long irq_flags; |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 141 | unsigned long *entry; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 142 | int i, rc = 0; |
| 143 | |
| 144 | if (!nr_pages) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | spin_lock_irqsave(&zdev->dma_table_lock, irq_flags); |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 148 | if (!zdev->dma_table) { |
| 149 | rc = -EINVAL; |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 150 | goto out_unlock; |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 151 | } |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 152 | |
| 153 | for (i = 0; i < nr_pages; i++) { |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 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); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 160 | page_addr += PAGE_SIZE; |
| 161 | dma_addr += PAGE_SIZE; |
| 162 | } |
| 163 | |
Sebastian Ott | 66728ee | 2015-10-26 11:19:13 +0100 | [diff] [blame] | 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 | } |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 176 | out_unlock: |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 177 | spin_unlock_irqrestore(&zdev->dma_table_lock, irq_flags); |
| 178 | return rc; |
| 179 | } |
| 180 | |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 181 | static int __dma_purge_tlb(struct zpci_dev *zdev, dma_addr_t dma_addr, |
| 182 | size_t size, int flags) |
| 183 | { |
Sebastian Ott | a5f1005 | 2017-12-01 18:47:32 +0100 | [diff] [blame] | 184 | unsigned long irqflags; |
| 185 | int ret; |
| 186 | |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 187 | /* |
| 188 | * With zdev->tlb_refresh == 0, rpcit is not required to establish new |
| 189 | * translations when previously invalid translation-table entries are |
Sebastian Ott | 4f5359e | 2016-09-08 13:44:57 +0200 | [diff] [blame] | 190 | * validated. With lazy unmap, rpcit is skipped for previously valid |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 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 | */ |
Sebastian Ott | 4f5359e | 2016-09-08 13:44:57 +0200 | [diff] [blame] | 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 | } |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 201 | |
Sebastian Ott | a5f1005 | 2017-12-01 18:47:32 +0100 | [diff] [blame] | 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; |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int dma_update_trans(struct zpci_dev *zdev, unsigned long pa, |
| 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 | |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 236 | void dma_free_seg_table(unsigned long entry) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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 | |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 248 | void dma_cleanup_tables(unsigned long *table) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 249 | { |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 250 | int rtx; |
| 251 | |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 252 | if (!table) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 253 | return; |
| 254 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 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); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 262 | static unsigned long __dma_alloc_iommu(struct device *dev, |
Gerald Schaefer | 5ec6d49 | 2014-02-18 19:47:17 +0100 | [diff] [blame] | 263 | unsigned long start, int size) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 264 | { |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 265 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Gerald Schaefer | 5ec6d49 | 2014-02-18 19:47:17 +0100 | [diff] [blame] | 266 | unsigned long boundary_size; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 267 | |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 268 | boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, |
Gerald Schaefer | 5ec6d49 | 2014-02-18 19:47:17 +0100 | [diff] [blame] | 269 | PAGE_SIZE) >> PAGE_SHIFT; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 270 | return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages, |
Sebastian Ott | 8ee2db3 | 2016-06-03 19:05:38 +0200 | [diff] [blame] | 271 | start, size, zdev->start_dma >> PAGE_SHIFT, |
| 272 | boundary_size, 0); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 275 | static dma_addr_t dma_alloc_address(struct device *dev, int size) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 276 | { |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 277 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 278 | unsigned long offset, flags; |
| 279 | |
| 280 | spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags); |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 281 | offset = __dma_alloc_iommu(dev, zdev->next_bit, size); |
Gerald Schaefer | c60d1ae | 2014-07-18 17:37:08 +0200 | [diff] [blame] | 282 | if (offset == -1) { |
Sebastian Ott | 4f5359e | 2016-09-08 13:44:57 +0200 | [diff] [blame] | 283 | if (!s390_iommu_strict) { |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 284 | /* global flush before DMA addresses are reused */ |
| 285 | if (zpci_refresh_global(zdev)) |
| 286 | goto out_error; |
| 287 | |
| 288 | bitmap_andnot(zdev->iommu_bitmap, zdev->iommu_bitmap, |
| 289 | zdev->lazy_bitmap, zdev->iommu_pages); |
| 290 | bitmap_zero(zdev->lazy_bitmap, zdev->iommu_pages); |
| 291 | } |
Gerald Schaefer | c60d1ae | 2014-07-18 17:37:08 +0200 | [diff] [blame] | 292 | /* wrap-around */ |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 293 | offset = __dma_alloc_iommu(dev, 0, size); |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 294 | if (offset == -1) |
| 295 | goto out_error; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 296 | } |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 297 | zdev->next_bit = offset + size; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 298 | spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags); |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 299 | |
| 300 | return zdev->start_dma + offset * PAGE_SIZE; |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 301 | |
| 302 | out_error: |
| 303 | spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags); |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 304 | return S390_MAPPING_ERROR; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 307 | static void dma_free_address(struct device *dev, dma_addr_t dma_addr, int size) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 308 | { |
Sebastian Ott | 9a99649 | 2016-01-29 15:13:30 +0100 | [diff] [blame] | 309 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 310 | unsigned long flags, offset; |
| 311 | |
| 312 | offset = (dma_addr - zdev->start_dma) >> PAGE_SHIFT; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 313 | |
| 314 | spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags); |
| 315 | if (!zdev->iommu_bitmap) |
| 316 | goto out; |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 317 | |
Sebastian Ott | 4f5359e | 2016-09-08 13:44:57 +0200 | [diff] [blame] | 318 | if (s390_iommu_strict) |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 319 | bitmap_clear(zdev->iommu_bitmap, offset, size); |
| 320 | else |
| 321 | bitmap_set(zdev->lazy_bitmap, offset, size); |
| 322 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 323 | out: |
| 324 | spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags); |
| 325 | } |
| 326 | |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 327 | static inline void zpci_err_dma(unsigned long rc, unsigned long addr) |
| 328 | { |
| 329 | struct { |
| 330 | unsigned long rc; |
| 331 | unsigned long addr; |
| 332 | } __packed data = {rc, addr}; |
| 333 | |
| 334 | zpci_err_hex(&data, sizeof(data)); |
| 335 | } |
| 336 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 337 | static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page, |
| 338 | unsigned long offset, size_t size, |
| 339 | enum dma_data_direction direction, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 340 | unsigned long attrs) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 341 | { |
Sebastian Ott | 198a527 | 2015-06-23 14:06:35 +0200 | [diff] [blame] | 342 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 343 | unsigned long pa = page_to_phys(page) + offset; |
| 344 | int flags = ZPCI_PTE_VALID; |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 345 | unsigned long nr_pages; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 346 | dma_addr_t dma_addr; |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 347 | int ret; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 348 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 349 | /* This rounds up number of pages based on size and offset */ |
| 350 | nr_pages = iommu_num_pages(pa, size, PAGE_SIZE); |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 351 | dma_addr = dma_alloc_address(dev, nr_pages); |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 352 | if (dma_addr == S390_MAPPING_ERROR) { |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 353 | ret = -ENOSPC; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 354 | goto out_err; |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 355 | } |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 356 | |
| 357 | /* Use rounded up size */ |
| 358 | size = nr_pages * PAGE_SIZE; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 359 | |
| 360 | if (direction == DMA_NONE || direction == DMA_TO_DEVICE) |
| 361 | flags |= ZPCI_TABLE_PROTECTED; |
| 362 | |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 363 | ret = dma_update_trans(zdev, pa, dma_addr, size, flags); |
| 364 | if (ret) |
| 365 | goto out_free; |
| 366 | |
| 367 | atomic64_add(nr_pages, &zdev->mapped_pages); |
| 368 | return dma_addr + (offset & ~PAGE_MASK); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 369 | |
| 370 | out_free: |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 371 | dma_free_address(dev, dma_addr, nr_pages); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 372 | out_err: |
Sebastian Ott | 1f1dcbd | 2013-10-22 15:17:19 +0200 | [diff] [blame] | 373 | zpci_err("map error:\n"); |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 374 | zpci_err_dma(ret, pa); |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 375 | return S390_MAPPING_ERROR; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr, |
| 379 | size_t size, enum dma_data_direction direction, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 380 | unsigned long attrs) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 381 | { |
Sebastian Ott | 198a527 | 2015-06-23 14:06:35 +0200 | [diff] [blame] | 382 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 383 | int npages, ret; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 384 | |
| 385 | npages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
| 386 | dma_addr = dma_addr & PAGE_MASK; |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 387 | ret = dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE, |
| 388 | ZPCI_PTE_INVALID); |
| 389 | if (ret) { |
Sebastian Ott | 1f1dcbd | 2013-10-22 15:17:19 +0200 | [diff] [blame] | 390 | zpci_err("unmap error:\n"); |
Sebastian Ott | 52d43d8 | 2015-10-26 11:20:44 +0100 | [diff] [blame] | 391 | zpci_err_dma(ret, dma_addr); |
| 392 | return; |
Sebastian Ott | 1f1dcbd | 2013-10-22 15:17:19 +0200 | [diff] [blame] | 393 | } |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 394 | |
Sebastian Ott | 6001018 | 2015-04-10 14:33:08 +0200 | [diff] [blame] | 395 | atomic64_add(npages, &zdev->unmapped_pages); |
Sebastian Ott | 8cb63b7 | 2016-08-17 13:51:11 +0200 | [diff] [blame] | 396 | dma_free_address(dev, dma_addr, npages); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void *s390_dma_alloc(struct device *dev, size_t size, |
| 400 | dma_addr_t *dma_handle, gfp_t flag, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 401 | unsigned long attrs) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 402 | { |
Sebastian Ott | 198a527 | 2015-06-23 14:06:35 +0200 | [diff] [blame] | 403 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 404 | struct page *page; |
| 405 | unsigned long pa; |
| 406 | dma_addr_t map; |
| 407 | |
| 408 | size = PAGE_ALIGN(size); |
| 409 | page = alloc_pages(flag, get_order(size)); |
| 410 | if (!page) |
| 411 | return NULL; |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 412 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 413 | pa = page_to_phys(page); |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 414 | map = s390_dma_map_pages(dev, page, 0, size, DMA_BIDIRECTIONAL, 0); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 415 | if (dma_mapping_error(dev, map)) { |
| 416 | free_pages(pa, get_order(size)); |
| 417 | return NULL; |
| 418 | } |
| 419 | |
Sebastian Ott | 6001018 | 2015-04-10 14:33:08 +0200 | [diff] [blame] | 420 | atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 421 | if (dma_handle) |
| 422 | *dma_handle = map; |
| 423 | return (void *) pa; |
| 424 | } |
| 425 | |
| 426 | static void s390_dma_free(struct device *dev, size_t size, |
| 427 | void *pa, dma_addr_t dma_handle, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 428 | unsigned long attrs) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 429 | { |
Sebastian Ott | 198a527 | 2015-06-23 14:06:35 +0200 | [diff] [blame] | 430 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
Sebastian Ott | f7038b7 | 2013-12-12 17:53:59 +0100 | [diff] [blame] | 431 | |
| 432 | size = PAGE_ALIGN(size); |
Sebastian Ott | 6001018 | 2015-04-10 14:33:08 +0200 | [diff] [blame] | 433 | atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages); |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 434 | s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, 0); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 435 | free_pages((unsigned long) pa, get_order(size)); |
| 436 | } |
| 437 | |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 438 | /* Map a segment into a contiguous dma address area */ |
| 439 | static int __s390_dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 440 | size_t size, dma_addr_t *handle, |
| 441 | enum dma_data_direction dir) |
| 442 | { |
Sebastian Ott | 6b7df3c | 2016-11-07 15:06:03 +0100 | [diff] [blame] | 443 | unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 444 | struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); |
| 445 | dma_addr_t dma_addr_base, dma_addr; |
| 446 | int flags = ZPCI_PTE_VALID; |
| 447 | struct scatterlist *s; |
Arnd Bergmann | 92dfffe | 2016-11-10 17:44:48 +0100 | [diff] [blame] | 448 | unsigned long pa = 0; |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 449 | int ret; |
| 450 | |
Sebastian Ott | 6b7df3c | 2016-11-07 15:06:03 +0100 | [diff] [blame] | 451 | dma_addr_base = dma_alloc_address(dev, nr_pages); |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 452 | if (dma_addr_base == S390_MAPPING_ERROR) |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 453 | return -ENOMEM; |
| 454 | |
| 455 | dma_addr = dma_addr_base; |
| 456 | if (dir == DMA_NONE || dir == DMA_TO_DEVICE) |
| 457 | flags |= ZPCI_TABLE_PROTECTED; |
| 458 | |
| 459 | for (s = sg; dma_addr < dma_addr_base + size; s = sg_next(s)) { |
Sebastian Ott | 6b7df3c | 2016-11-07 15:06:03 +0100 | [diff] [blame] | 460 | pa = page_to_phys(sg_page(s)); |
| 461 | ret = __dma_update_trans(zdev, pa, dma_addr, |
| 462 | s->offset + s->length, flags); |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 463 | if (ret) |
| 464 | goto unmap; |
| 465 | |
Sebastian Ott | 6b7df3c | 2016-11-07 15:06:03 +0100 | [diff] [blame] | 466 | dma_addr += s->offset + s->length; |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 467 | } |
Sebastian Ott | 1f166e9e | 2016-09-05 17:49:17 +0200 | [diff] [blame] | 468 | ret = __dma_purge_tlb(zdev, dma_addr_base, size, flags); |
| 469 | if (ret) |
| 470 | goto unmap; |
| 471 | |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 472 | *handle = dma_addr_base; |
Sebastian Ott | 6b7df3c | 2016-11-07 15:06:03 +0100 | [diff] [blame] | 473 | atomic64_add(nr_pages, &zdev->mapped_pages); |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 474 | |
| 475 | return ret; |
| 476 | |
| 477 | unmap: |
| 478 | dma_update_trans(zdev, 0, dma_addr_base, dma_addr - dma_addr_base, |
| 479 | ZPCI_PTE_INVALID); |
Sebastian Ott | 6b7df3c | 2016-11-07 15:06:03 +0100 | [diff] [blame] | 480 | dma_free_address(dev, dma_addr_base, nr_pages); |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 481 | zpci_err("map error:\n"); |
| 482 | zpci_err_dma(ret, pa); |
| 483 | return ret; |
| 484 | } |
| 485 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 486 | static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 487 | int nr_elements, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 488 | unsigned long attrs) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 489 | { |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 490 | struct scatterlist *s = sg, *start = sg, *dma = sg; |
| 491 | unsigned int max = dma_get_max_seg_size(dev); |
| 492 | unsigned int size = s->offset + s->length; |
| 493 | unsigned int offset = s->offset; |
| 494 | int count = 0, i; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 495 | |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 496 | for (i = 1; i < nr_elements; i++) { |
| 497 | s = sg_next(s); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 498 | |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 499 | s->dma_address = S390_MAPPING_ERROR; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 500 | s->dma_length = 0; |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 501 | |
| 502 | if (s->offset || (size & ~PAGE_MASK) || |
| 503 | size + s->length > max) { |
| 504 | if (__s390_dma_map_sg(dev, start, size, |
| 505 | &dma->dma_address, dir)) |
| 506 | goto unmap; |
| 507 | |
| 508 | dma->dma_address += offset; |
| 509 | dma->dma_length = size - offset; |
| 510 | |
| 511 | size = offset = s->offset; |
| 512 | start = s; |
| 513 | dma = sg_next(dma); |
| 514 | count++; |
| 515 | } |
| 516 | size += s->length; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 517 | } |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 518 | if (__s390_dma_map_sg(dev, start, size, &dma->dma_address, dir)) |
| 519 | goto unmap; |
| 520 | |
| 521 | dma->dma_address += offset; |
| 522 | dma->dma_length = size - offset; |
| 523 | |
| 524 | return count + 1; |
| 525 | unmap: |
| 526 | for_each_sg(sg, s, count, i) |
| 527 | s390_dma_unmap_pages(dev, sg_dma_address(s), sg_dma_len(s), |
| 528 | dir, attrs); |
| 529 | |
| 530 | return 0; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 534 | int nr_elements, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 535 | unsigned long attrs) |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 536 | { |
| 537 | struct scatterlist *s; |
| 538 | int i; |
| 539 | |
| 540 | for_each_sg(sg, s, nr_elements, i) { |
Sebastian Ott | ee877b8 | 2016-08-19 09:12:09 +0200 | [diff] [blame] | 541 | if (s->dma_length) |
| 542 | s390_dma_unmap_pages(dev, s->dma_address, s->dma_length, |
| 543 | dir, attrs); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 544 | s->dma_address = 0; |
| 545 | s->dma_length = 0; |
| 546 | } |
| 547 | } |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 548 | |
| 549 | static int s390_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 550 | { |
| 551 | return dma_addr == S390_MAPPING_ERROR; |
| 552 | } |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 553 | |
| 554 | int zpci_dma_init_device(struct zpci_dev *zdev) |
| 555 | { |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 556 | int rc; |
| 557 | |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 558 | /* |
| 559 | * At this point, if the device is part of an IOMMU domain, this would |
| 560 | * be a strong hint towards a bug in the IOMMU API (common) code and/or |
| 561 | * simultaneous access via IOMMU and DMA API. So let's issue a warning. |
| 562 | */ |
| 563 | WARN_ON(zdev->s390_domain); |
| 564 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 565 | spin_lock_init(&zdev->iommu_bitmap_lock); |
| 566 | spin_lock_init(&zdev->dma_table_lock); |
| 567 | |
| 568 | zdev->dma_table = dma_alloc_cpu_table(); |
| 569 | if (!zdev->dma_table) { |
| 570 | rc = -ENOMEM; |
Sebastian Ott | dba5990 | 2016-04-15 09:41:35 +0200 | [diff] [blame] | 571 | goto out; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Gerald Schaefer | 69eea95 | 2015-11-16 14:35:48 +0100 | [diff] [blame] | 574 | /* |
| 575 | * Restrict the iommu bitmap size to the minimum of the following: |
| 576 | * - main memory size |
| 577 | * - 3-level pagetable address limit minus start_dma offset |
| 578 | * - DMA address range allowed by the hardware (clp query pci fn) |
| 579 | * |
| 580 | * Also set zdev->end_dma to the actual end address of the usable |
| 581 | * range, instead of the theoretical maximum as reported by hardware. |
| 582 | */ |
Sebastian Ott | 53b1bc9 | 2016-06-03 19:03:12 +0200 | [diff] [blame] | 583 | zdev->start_dma = PAGE_ALIGN(zdev->start_dma); |
Gerald Schaefer | 69eea95 | 2015-11-16 14:35:48 +0100 | [diff] [blame] | 584 | zdev->iommu_size = min3((u64) high_memory, |
| 585 | ZPCI_TABLE_SIZE_RT - zdev->start_dma, |
| 586 | zdev->end_dma - zdev->start_dma + 1); |
| 587 | zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 588 | zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT; |
Sebastian Ott | 2245932 | 2013-08-29 20:31:50 +0200 | [diff] [blame] | 589 | zdev->iommu_bitmap = vzalloc(zdev->iommu_pages / 8); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 590 | if (!zdev->iommu_bitmap) { |
| 591 | rc = -ENOMEM; |
Sebastian Ott | dba5990 | 2016-04-15 09:41:35 +0200 | [diff] [blame] | 592 | goto free_dma_table; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 593 | } |
Sebastian Ott | 4f5359e | 2016-09-08 13:44:57 +0200 | [diff] [blame] | 594 | if (!s390_iommu_strict) { |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 595 | zdev->lazy_bitmap = vzalloc(zdev->iommu_pages / 8); |
| 596 | if (!zdev->lazy_bitmap) { |
| 597 | rc = -ENOMEM; |
| 598 | goto free_bitmap; |
| 599 | } |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 600 | |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 601 | } |
Gerald Schaefer | 69eea95 | 2015-11-16 14:35:48 +0100 | [diff] [blame] | 602 | rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 603 | (u64) zdev->dma_table); |
| 604 | if (rc) |
Sebastian Ott | dba5990 | 2016-04-15 09:41:35 +0200 | [diff] [blame] | 605 | goto free_bitmap; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 606 | |
Sebastian Ott | dba5990 | 2016-04-15 09:41:35 +0200 | [diff] [blame] | 607 | return 0; |
| 608 | free_bitmap: |
| 609 | vfree(zdev->iommu_bitmap); |
| 610 | zdev->iommu_bitmap = NULL; |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 611 | vfree(zdev->lazy_bitmap); |
| 612 | zdev->lazy_bitmap = NULL; |
Sebastian Ott | dba5990 | 2016-04-15 09:41:35 +0200 | [diff] [blame] | 613 | free_dma_table: |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 614 | dma_free_cpu_table(zdev->dma_table); |
Sebastian Ott | dba5990 | 2016-04-15 09:41:35 +0200 | [diff] [blame] | 615 | zdev->dma_table = NULL; |
| 616 | out: |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 617 | return rc; |
| 618 | } |
| 619 | |
| 620 | void zpci_dma_exit_device(struct zpci_dev *zdev) |
| 621 | { |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 622 | /* |
| 623 | * At this point, if the device is part of an IOMMU domain, this would |
| 624 | * be a strong hint towards a bug in the IOMMU API (common) code and/or |
| 625 | * simultaneous access via IOMMU and DMA API. So let's issue a warning. |
| 626 | */ |
| 627 | WARN_ON(zdev->s390_domain); |
| 628 | |
Sebastian Ott | 7257083 | 2017-06-10 14:10:00 +0200 | [diff] [blame] | 629 | if (zpci_unregister_ioat(zdev, 0)) |
| 630 | return; |
| 631 | |
Gerald Schaefer | 8128f23c | 2015-08-27 15:33:03 +0200 | [diff] [blame] | 632 | dma_cleanup_tables(zdev->dma_table); |
| 633 | zdev->dma_table = NULL; |
Sebastian Ott | 2245932 | 2013-08-29 20:31:50 +0200 | [diff] [blame] | 634 | vfree(zdev->iommu_bitmap); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 635 | zdev->iommu_bitmap = NULL; |
Sebastian Ott | 13954fd | 2016-09-08 13:25:01 +0200 | [diff] [blame] | 636 | vfree(zdev->lazy_bitmap); |
| 637 | zdev->lazy_bitmap = NULL; |
| 638 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 639 | zdev->next_bit = 0; |
| 640 | } |
| 641 | |
| 642 | static int __init dma_alloc_cpu_table_caches(void) |
| 643 | { |
| 644 | dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables", |
| 645 | ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN, |
| 646 | 0, NULL); |
| 647 | if (!dma_region_table_cache) |
| 648 | return -ENOMEM; |
| 649 | |
| 650 | dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables", |
| 651 | ZPCI_PT_SIZE, ZPCI_PT_ALIGN, |
| 652 | 0, NULL); |
| 653 | if (!dma_page_table_cache) { |
| 654 | kmem_cache_destroy(dma_region_table_cache); |
| 655 | return -ENOMEM; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | int __init zpci_dma_init(void) |
| 661 | { |
| 662 | return dma_alloc_cpu_table_caches(); |
| 663 | } |
| 664 | |
| 665 | void zpci_dma_exit(void) |
| 666 | { |
| 667 | kmem_cache_destroy(dma_page_table_cache); |
| 668 | kmem_cache_destroy(dma_region_table_cache); |
| 669 | } |
| 670 | |
| 671 | #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16) |
| 672 | |
| 673 | static int __init dma_debug_do_init(void) |
| 674 | { |
| 675 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 676 | return 0; |
| 677 | } |
| 678 | fs_initcall(dma_debug_do_init); |
| 679 | |
Bart Van Assche | 5299709 | 2017-01-20 13:04:01 -0800 | [diff] [blame] | 680 | const struct dma_map_ops s390_pci_dma_ops = { |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 681 | .alloc = s390_dma_alloc, |
| 682 | .free = s390_dma_free, |
| 683 | .map_sg = s390_dma_map_sg, |
| 684 | .unmap_sg = s390_dma_unmap_sg, |
| 685 | .map_page = s390_dma_map_pages, |
| 686 | .unmap_page = s390_dma_unmap_pages, |
Christoph Hellwig | 81cac18 | 2017-05-21 13:04:09 +0200 | [diff] [blame] | 687 | .mapping_error = s390_mapping_error, |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 688 | /* if we support direct DMA this must be conditional */ |
| 689 | .is_phys = 0, |
| 690 | /* dma_supported is unconditionally true without a callback */ |
| 691 | }; |
Christian Borntraeger | e82becfc | 2016-02-02 21:46:34 -0800 | [diff] [blame] | 692 | EXPORT_SYMBOL_GPL(s390_pci_dma_ops); |
Gerald Schaefer | c60d1ae | 2014-07-18 17:37:08 +0200 | [diff] [blame] | 693 | |
| 694 | static int __init s390_iommu_setup(char *str) |
| 695 | { |
| 696 | if (!strncmp(str, "strict", 6)) |
| 697 | s390_iommu_strict = 1; |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | __setup("s390_iommu=", s390_iommu_setup); |