blob: 203caacf22422fc0361e5a97b9b34bda0058b58c [file] [log] [blame]
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +10001/*
2 * VFIO: IOMMU DMA mapping support for TCE on POWER
3 *
4 * Copyright (C) 2013 IBM Corp. All rights reserved.
5 * Author: Alexey Kardashevskiy <aik@ozlabs.ru>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio_iommu_type1.c:
12 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
13 * Author: Alex Williamson <alex.williamson@redhat.com>
14 */
15
16#include <linux/module.h>
17#include <linux/pci.h>
18#include <linux/slab.h>
19#include <linux/uaccess.h>
20#include <linux/err.h>
21#include <linux/vfio.h>
22#include <asm/iommu.h>
23#include <asm/tce.h>
24
25#define DRIVER_VERSION "0.1"
26#define DRIVER_AUTHOR "aik@ozlabs.ru"
27#define DRIVER_DESC "VFIO IOMMU SPAPR TCE"
28
29static void tce_iommu_detach_group(void *iommu_data,
30 struct iommu_group *iommu_group);
31
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +100032static long try_increment_locked_vm(long npages)
33{
34 long ret = 0, locked, lock_limit;
35
36 if (!current || !current->mm)
37 return -ESRCH; /* process exited */
38
39 if (!npages)
40 return 0;
41
42 down_write(&current->mm->mmap_sem);
43 locked = current->mm->locked_vm + npages;
44 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
45 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
46 ret = -ENOMEM;
47 else
48 current->mm->locked_vm += npages;
49
50 pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%ld%s\n", current->pid,
51 npages << PAGE_SHIFT,
52 current->mm->locked_vm << PAGE_SHIFT,
53 rlimit(RLIMIT_MEMLOCK),
54 ret ? " - exceeded" : "");
55
56 up_write(&current->mm->mmap_sem);
57
58 return ret;
59}
60
61static void decrement_locked_vm(long npages)
62{
63 if (!current || !current->mm || !npages)
64 return; /* process exited */
65
66 down_write(&current->mm->mmap_sem);
67 if (WARN_ON_ONCE(npages > current->mm->locked_vm))
68 npages = current->mm->locked_vm;
69 current->mm->locked_vm -= npages;
70 pr_debug("[%d] RLIMIT_MEMLOCK -%ld %ld/%ld\n", current->pid,
71 npages << PAGE_SHIFT,
72 current->mm->locked_vm << PAGE_SHIFT,
73 rlimit(RLIMIT_MEMLOCK));
74 up_write(&current->mm->mmap_sem);
75}
76
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +100077/*
78 * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
79 *
80 * This code handles mapping and unmapping of user data buffers
81 * into DMA'ble space using the IOMMU
82 */
83
84/*
85 * The container descriptor supports only a single group per container.
86 * Required by the API as the container is not supplied with the IOMMU group
87 * at the moment of initialization.
88 */
89struct tce_container {
90 struct mutex lock;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +100091 struct iommu_group *grp;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +100092 bool enabled;
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +100093 unsigned long locked_pages;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +100094};
95
Alexey Kardashevskiye432bc72015-06-05 16:34:59 +100096static bool tce_page_is_contained(struct page *page, unsigned page_shift)
97{
98 /*
99 * Check that the TCE table granularity is not bigger than the size of
100 * a page we just found. Otherwise the hardware can get access to
101 * a bigger memory chunk that it should.
102 */
103 return (PAGE_SHIFT + compound_order(compound_head(page))) >= page_shift;
104}
105
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000106static long tce_iommu_find_table(struct tce_container *container,
107 phys_addr_t ioba, struct iommu_table **ptbl)
108{
109 long i;
110 struct iommu_table_group *table_group;
111
112 table_group = iommu_group_get_iommudata(container->grp);
113 if (!table_group)
114 return -1;
115
116 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
117 struct iommu_table *tbl = table_group->tables[i];
118
119 if (tbl) {
120 unsigned long entry = ioba >> tbl->it_page_shift;
121 unsigned long start = tbl->it_offset;
122 unsigned long end = start + tbl->it_size;
123
124 if ((start <= entry) && (entry < end)) {
125 *ptbl = tbl;
126 return i;
127 }
128 }
129 }
130
131 return -1;
132}
133
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000134static int tce_iommu_enable(struct tce_container *container)
135{
136 int ret = 0;
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +1000137 unsigned long locked;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000138 struct iommu_table_group *table_group;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000139
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000140 if (!container->grp)
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000141 return -ENXIO;
142
143 if (!current->mm)
144 return -ESRCH; /* process exited */
145
146 if (container->enabled)
147 return -EBUSY;
148
149 /*
150 * When userspace pages are mapped into the IOMMU, they are effectively
151 * locked memory, so, theoretically, we need to update the accounting
152 * of locked pages on each map and unmap. For powerpc, the map unmap
153 * paths can be very hot, though, and the accounting would kill
154 * performance, especially since it would be difficult to impossible
155 * to handle the accounting in real mode only.
156 *
157 * To address that, rather than precisely accounting every page, we
158 * instead account for a worst case on locked memory when the iommu is
159 * enabled and disabled. The worst case upper bound on locked memory
160 * is the size of the whole iommu window, which is usually relatively
161 * small (compared to total memory sizes) on POWER hardware.
162 *
163 * Also we don't have a nice way to fail on H_PUT_TCE due to ulimits,
164 * that would effectively kill the guest at random points, much better
165 * enforcing the limit based on the max that the guest can map.
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +1000166 *
167 * Unfortunately at the moment it counts whole tables, no matter how
168 * much memory the guest has. I.e. for 4GB guest and 4 IOMMU groups
169 * each with 2GB DMA window, 8GB will be counted here. The reason for
170 * this is that we cannot tell here the amount of RAM used by the guest
171 * as this information is only available from KVM and VFIO is
172 * KVM agnostic.
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +1000173 *
174 * So we do not allow enabling a container without a group attached
175 * as there is no way to know how much we should increment
176 * the locked_vm counter.
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000177 */
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000178 table_group = iommu_group_get_iommudata(container->grp);
179 if (!table_group)
180 return -ENODEV;
181
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +1000182 if (!table_group->tce32_size)
183 return -EPERM;
184
185 locked = table_group->tce32_size >> PAGE_SHIFT;
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +1000186 ret = try_increment_locked_vm(locked);
187 if (ret)
188 return ret;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000189
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +1000190 container->locked_pages = locked;
191
192 container->enabled = true;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000193
194 return ret;
195}
196
197static void tce_iommu_disable(struct tce_container *container)
198{
199 if (!container->enabled)
200 return;
201
202 container->enabled = false;
203
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +1000204 if (!current->mm)
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000205 return;
206
Alexey Kardashevskiy2d270df2015-06-05 16:35:01 +1000207 decrement_locked_vm(container->locked_pages);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000208}
209
210static void *tce_iommu_open(unsigned long arg)
211{
212 struct tce_container *container;
213
214 if (arg != VFIO_SPAPR_TCE_IOMMU) {
215 pr_err("tce_vfio: Wrong IOMMU type\n");
216 return ERR_PTR(-EINVAL);
217 }
218
219 container = kzalloc(sizeof(*container), GFP_KERNEL);
220 if (!container)
221 return ERR_PTR(-ENOMEM);
222
223 mutex_init(&container->lock);
224
225 return container;
226}
227
228static void tce_iommu_release(void *iommu_data)
229{
230 struct tce_container *container = iommu_data;
231
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000232 WARN_ON(container->grp);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000233
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000234 if (container->grp)
235 tce_iommu_detach_group(iommu_data, container->grp);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000236
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000237 tce_iommu_disable(container);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000238 mutex_destroy(&container->lock);
239
240 kfree(container);
241}
242
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000243static void tce_iommu_unuse_page(struct tce_container *container,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000244 unsigned long hpa)
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000245{
246 struct page *page;
247
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000248 page = pfn_to_page(hpa >> PAGE_SHIFT);
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000249 put_page(page);
250}
251
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000252static int tce_iommu_clear(struct tce_container *container,
253 struct iommu_table *tbl,
254 unsigned long entry, unsigned long pages)
255{
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000256 unsigned long oldhpa;
257 long ret;
258 enum dma_data_direction direction;
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000259
260 for ( ; pages; --pages, ++entry) {
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000261 direction = DMA_NONE;
262 oldhpa = 0;
263 ret = iommu_tce_xchg(tbl, entry, &oldhpa, &direction);
264 if (ret)
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000265 continue;
266
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000267 if (direction == DMA_NONE)
268 continue;
269
270 tce_iommu_unuse_page(container, oldhpa);
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000271 }
272
273 return 0;
274}
275
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000276static int tce_iommu_use_page(unsigned long tce, unsigned long *hpa)
277{
278 struct page *page = NULL;
279 enum dma_data_direction direction = iommu_tce_direction(tce);
280
281 if (get_user_pages_fast(tce & PAGE_MASK, 1,
282 direction != DMA_TO_DEVICE, &page) != 1)
283 return -EFAULT;
284
285 *hpa = __pa((unsigned long) page_address(page));
286
287 return 0;
288}
289
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000290static long tce_iommu_build(struct tce_container *container,
291 struct iommu_table *tbl,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000292 unsigned long entry, unsigned long tce, unsigned long pages,
293 enum dma_data_direction direction)
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000294{
295 long i, ret = 0;
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000296 struct page *page;
297 unsigned long hpa;
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000298 enum dma_data_direction dirtmp;
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000299
300 for (i = 0; i < pages; ++i) {
301 unsigned long offset = tce & IOMMU_PAGE_MASK(tbl) & ~PAGE_MASK;
302
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000303 ret = tce_iommu_use_page(tce, &hpa);
304 if (ret)
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000305 break;
Alexey Kardashevskiye432bc72015-06-05 16:34:59 +1000306
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000307 page = pfn_to_page(hpa >> PAGE_SHIFT);
Alexey Kardashevskiye432bc72015-06-05 16:34:59 +1000308 if (!tce_page_is_contained(page, tbl->it_page_shift)) {
309 ret = -EPERM;
310 break;
311 }
312
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000313 hpa |= offset;
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000314 dirtmp = direction;
315 ret = iommu_tce_xchg(tbl, entry + i, &hpa, &dirtmp);
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000316 if (ret) {
Alexey Kardashevskiy649354b2015-06-05 16:35:03 +1000317 tce_iommu_unuse_page(container, hpa);
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000318 pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
319 __func__, entry << tbl->it_page_shift,
320 tce, ret);
321 break;
322 }
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000323
324 if (dirtmp != DMA_NONE)
325 tce_iommu_unuse_page(container, hpa);
326
Alexey Kardashevskiy00663d42015-06-05 16:35:00 +1000327 tce += IOMMU_PAGE_SIZE(tbl);
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000328 }
329
330 if (ret)
331 tce_iommu_clear(container, tbl, entry, i);
332
333 return ret;
334}
335
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +1000336static long tce_iommu_create_table(struct tce_container *container,
337 struct iommu_table_group *table_group,
338 int num,
339 __u32 page_shift,
340 __u64 window_size,
341 __u32 levels,
342 struct iommu_table **ptbl)
343{
344 long ret, table_size;
345
346 table_size = table_group->ops->get_table_size(page_shift, window_size,
347 levels);
348 if (!table_size)
349 return -EINVAL;
350
351 ret = try_increment_locked_vm(table_size >> PAGE_SHIFT);
352 if (ret)
353 return ret;
354
355 ret = table_group->ops->create_table(table_group, num,
356 page_shift, window_size, levels, ptbl);
357
358 WARN_ON(!ret && !(*ptbl)->it_ops->free);
359 WARN_ON(!ret && ((*ptbl)->it_allocated_size != table_size));
360
361 if (ret)
362 decrement_locked_vm(table_size >> PAGE_SHIFT);
363
364 return ret;
365}
366
367static void tce_iommu_free_table(struct iommu_table *tbl)
368{
369 unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT;
370
371 tbl->it_ops->free(tbl);
372 decrement_locked_vm(pages);
373}
374
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000375static long tce_iommu_ioctl(void *iommu_data,
376 unsigned int cmd, unsigned long arg)
377{
378 struct tce_container *container = iommu_data;
379 unsigned long minsz;
380 long ret;
381
382 switch (cmd) {
383 case VFIO_CHECK_EXTENSION:
Gavin Shan1b69be52014-06-10 11:41:57 +1000384 switch (arg) {
385 case VFIO_SPAPR_TCE_IOMMU:
386 ret = 1;
387 break;
388 default:
389 ret = vfio_spapr_iommu_eeh_ioctl(NULL, cmd, arg);
390 break;
391 }
392
393 return (ret < 0) ? 0 : ret;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000394
395 case VFIO_IOMMU_SPAPR_TCE_GET_INFO: {
396 struct vfio_iommu_spapr_tce_info info;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000397 struct iommu_table_group *table_group;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000398
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000399 if (WARN_ON(!container->grp))
400 return -ENXIO;
401
402 table_group = iommu_group_get_iommudata(container->grp);
403
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +1000404 if (!table_group)
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000405 return -ENXIO;
406
407 minsz = offsetofend(struct vfio_iommu_spapr_tce_info,
408 dma32_window_size);
409
410 if (copy_from_user(&info, (void __user *)arg, minsz))
411 return -EFAULT;
412
413 if (info.argsz < minsz)
414 return -EINVAL;
415
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +1000416 info.dma32_window_start = table_group->tce32_start;
417 info.dma32_window_size = table_group->tce32_size;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000418 info.flags = 0;
419
420 if (copy_to_user((void __user *)arg, &info, minsz))
421 return -EFAULT;
422
423 return 0;
424 }
425 case VFIO_IOMMU_MAP_DMA: {
426 struct vfio_iommu_type1_dma_map param;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000427 struct iommu_table *tbl = NULL;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000428 long num;
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000429 enum dma_data_direction direction;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000430
Alexey Kardashevskiy3c56e822015-06-05 16:35:02 +1000431 if (!container->enabled)
432 return -EPERM;
433
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000434 minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
435
436 if (copy_from_user(&param, (void __user *)arg, minsz))
437 return -EFAULT;
438
439 if (param.argsz < minsz)
440 return -EINVAL;
441
442 if (param.flags & ~(VFIO_DMA_MAP_FLAG_READ |
443 VFIO_DMA_MAP_FLAG_WRITE))
444 return -EINVAL;
445
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000446 num = tce_iommu_find_table(container, param.iova, &tbl);
447 if (num < 0)
448 return -ENXIO;
449
Alexey Kardashevskiy00663d42015-06-05 16:35:00 +1000450 if ((param.size & ~IOMMU_PAGE_MASK(tbl)) ||
451 (param.vaddr & ~IOMMU_PAGE_MASK(tbl)))
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000452 return -EINVAL;
453
454 /* iova is checked by the IOMMU API */
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000455 if (param.flags & VFIO_DMA_MAP_FLAG_READ) {
456 if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
457 direction = DMA_BIDIRECTIONAL;
458 else
459 direction = DMA_TO_DEVICE;
460 } else {
461 if (param.flags & VFIO_DMA_MAP_FLAG_WRITE)
462 direction = DMA_FROM_DEVICE;
463 else
464 return -EINVAL;
465 }
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000466
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000467 ret = iommu_tce_put_param_check(tbl, param.iova, param.vaddr);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000468 if (ret)
469 return ret;
470
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000471 ret = tce_iommu_build(container, tbl,
Alexey Kardashevskiy00663d42015-06-05 16:35:00 +1000472 param.iova >> tbl->it_page_shift,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +1000473 param.vaddr,
474 param.size >> tbl->it_page_shift,
475 direction);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000476
477 iommu_flush_tce(tbl);
478
479 return ret;
480 }
481 case VFIO_IOMMU_UNMAP_DMA: {
482 struct vfio_iommu_type1_dma_unmap param;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000483 struct iommu_table *tbl = NULL;
484 long num;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000485
Alexey Kardashevskiy3c56e822015-06-05 16:35:02 +1000486 if (!container->enabled)
487 return -EPERM;
488
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000489 minsz = offsetofend(struct vfio_iommu_type1_dma_unmap,
490 size);
491
492 if (copy_from_user(&param, (void __user *)arg, minsz))
493 return -EFAULT;
494
495 if (param.argsz < minsz)
496 return -EINVAL;
497
498 /* No flag is supported now */
499 if (param.flags)
500 return -EINVAL;
501
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000502 num = tce_iommu_find_table(container, param.iova, &tbl);
503 if (num < 0)
504 return -ENXIO;
505
Alexey Kardashevskiy00663d42015-06-05 16:35:00 +1000506 if (param.size & ~IOMMU_PAGE_MASK(tbl))
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000507 return -EINVAL;
508
509 ret = iommu_tce_clear_param_check(tbl, param.iova, 0,
Alexey Kardashevskiy00663d42015-06-05 16:35:00 +1000510 param.size >> tbl->it_page_shift);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000511 if (ret)
512 return ret;
513
Alexey Kardashevskiy9b14a1f2015-06-05 16:34:58 +1000514 ret = tce_iommu_clear(container, tbl,
Alexey Kardashevskiy00663d42015-06-05 16:35:00 +1000515 param.iova >> tbl->it_page_shift,
516 param.size >> tbl->it_page_shift);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000517 iommu_flush_tce(tbl);
518
519 return ret;
520 }
521 case VFIO_IOMMU_ENABLE:
522 mutex_lock(&container->lock);
523 ret = tce_iommu_enable(container);
524 mutex_unlock(&container->lock);
525 return ret;
526
527
528 case VFIO_IOMMU_DISABLE:
529 mutex_lock(&container->lock);
530 tce_iommu_disable(container);
531 mutex_unlock(&container->lock);
532 return 0;
Gavin Shan1b69be52014-06-10 11:41:57 +1000533 case VFIO_EEH_PE_OP:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000534 if (!container->grp)
Gavin Shan1b69be52014-06-10 11:41:57 +1000535 return -ENODEV;
536
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000537 return vfio_spapr_iommu_eeh_ioctl(container->grp,
538 cmd, arg);
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000539 }
540
541 return -ENOTTY;
542}
543
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +1000544static void tce_iommu_release_ownership(struct tce_container *container,
545 struct iommu_table_group *table_group)
546{
547 int i;
548
549 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
550 struct iommu_table *tbl = table_group->tables[i];
551
552 if (!tbl)
553 continue;
554
555 tce_iommu_clear(container, tbl, tbl->it_offset, tbl->it_size);
556 if (tbl->it_map)
557 iommu_release_ownership(tbl);
558 }
559}
560
561static int tce_iommu_take_ownership(struct tce_container *container,
562 struct iommu_table_group *table_group)
563{
564 int i, j, rc = 0;
565
566 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
567 struct iommu_table *tbl = table_group->tables[i];
568
569 if (!tbl || !tbl->it_map)
570 continue;
571
572 rc = iommu_take_ownership(tbl);
573 if (rc) {
574 for (j = 0; j < i; ++j)
575 iommu_release_ownership(
576 table_group->tables[j]);
577
578 return rc;
579 }
580 }
581
582 return 0;
583}
584
585static void tce_iommu_release_ownership_ddw(struct tce_container *container,
586 struct iommu_table_group *table_group)
587{
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +1000588 long i;
589
590 if (!table_group->ops->unset_window) {
591 WARN_ON_ONCE(1);
592 return;
593 }
594
595 for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
596 /* Store table pointer as unset_window resets it */
597 struct iommu_table *tbl = table_group->tables[i];
598
599 if (!tbl)
600 continue;
601
602 table_group->ops->unset_window(table_group, i);
603 tce_iommu_clear(container, tbl,
604 tbl->it_offset, tbl->it_size);
605 tce_iommu_free_table(tbl);
606 }
607
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +1000608 table_group->ops->release_ownership(table_group);
609}
610
611static long tce_iommu_take_ownership_ddw(struct tce_container *container,
612 struct iommu_table_group *table_group)
613{
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +1000614 long ret;
615 struct iommu_table *tbl = NULL;
616
617 if (!table_group->ops->create_table || !table_group->ops->set_window ||
618 !table_group->ops->release_ownership) {
619 WARN_ON_ONCE(1);
620 return -EFAULT;
621 }
622
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +1000623 table_group->ops->take_ownership(table_group);
624
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +1000625 ret = tce_iommu_create_table(container,
626 table_group,
627 0, /* window number */
628 IOMMU_PAGE_SHIFT_4K,
629 table_group->tce32_size,
630 1, /* default levels */
631 &tbl);
632 if (!ret) {
633 ret = table_group->ops->set_window(table_group, 0, tbl);
634 if (ret)
635 tce_iommu_free_table(tbl);
636 else
637 table_group->tables[0] = tbl;
638 }
639
640 if (ret)
641 table_group->ops->release_ownership(table_group);
642
643 return ret;
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +1000644}
645
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000646static int tce_iommu_attach_group(void *iommu_data,
647 struct iommu_group *iommu_group)
648{
649 int ret;
650 struct tce_container *container = iommu_data;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000651 struct iommu_table_group *table_group;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000652
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000653 mutex_lock(&container->lock);
654
655 /* pr_debug("tce_vfio: Attaching group #%u to iommu %p\n",
656 iommu_group_id(iommu_group), iommu_group); */
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000657 if (container->grp) {
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000658 pr_warn("tce_vfio: Only one group per IOMMU container is allowed, existing id=%d, attaching id=%d\n",
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000659 iommu_group_id(container->grp),
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000660 iommu_group_id(iommu_group));
661 ret = -EBUSY;
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000662 goto unlock_exit;
663 }
664
665 if (container->enabled) {
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000666 pr_err("tce_vfio: attaching group #%u to enabled container\n",
667 iommu_group_id(iommu_group));
668 ret = -EBUSY;
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000669 goto unlock_exit;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000670 }
671
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000672 table_group = iommu_group_get_iommudata(iommu_group);
673 if (!table_group) {
674 ret = -ENXIO;
675 goto unlock_exit;
676 }
677
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +1000678 if (!table_group->ops || !table_group->ops->take_ownership ||
679 !table_group->ops->release_ownership)
680 ret = tce_iommu_take_ownership(container, table_group);
681 else
682 ret = tce_iommu_take_ownership_ddw(container, table_group);
683
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000684 if (!ret)
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000685 container->grp = iommu_group;
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000686
687unlock_exit:
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000688 mutex_unlock(&container->lock);
689
690 return ret;
691}
692
693static void tce_iommu_detach_group(void *iommu_data,
694 struct iommu_group *iommu_group)
695{
696 struct tce_container *container = iommu_data;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000697 struct iommu_table_group *table_group;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000698
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000699 mutex_lock(&container->lock);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000700 if (iommu_group != container->grp) {
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000701 pr_warn("tce_vfio: detaching group #%u, expected group is #%u\n",
702 iommu_group_id(iommu_group),
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000703 iommu_group_id(container->grp));
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000704 goto unlock_exit;
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000705 }
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000706
707 if (container->enabled) {
708 pr_warn("tce_vfio: detaching group #%u from enabled container, forcing disable\n",
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000709 iommu_group_id(container->grp));
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000710 tce_iommu_disable(container);
711 }
712
713 /* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
714 iommu_group_id(iommu_group), iommu_group); */
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +1000715 container->grp = NULL;
716
717 table_group = iommu_group_get_iommudata(iommu_group);
718 BUG_ON(!table_group);
719
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +1000720 if (!table_group->ops || !table_group->ops->release_ownership)
721 tce_iommu_release_ownership(container, table_group);
722 else
723 tce_iommu_release_ownership_ddw(container, table_group);
Alexey Kardashevskiy22af4852015-06-05 16:35:04 +1000724
725unlock_exit:
Alexey Kardashevskiy5ffd2292013-05-21 13:33:10 +1000726 mutex_unlock(&container->lock);
727}
728
729const struct vfio_iommu_driver_ops tce_iommu_driver_ops = {
730 .name = "iommu-vfio-powerpc",
731 .owner = THIS_MODULE,
732 .open = tce_iommu_open,
733 .release = tce_iommu_release,
734 .ioctl = tce_iommu_ioctl,
735 .attach_group = tce_iommu_attach_group,
736 .detach_group = tce_iommu_detach_group,
737};
738
739static int __init tce_iommu_init(void)
740{
741 return vfio_register_iommu_driver(&tce_iommu_driver_ops);
742}
743
744static void __exit tce_iommu_cleanup(void)
745{
746 vfio_unregister_iommu_driver(&tce_iommu_driver_ops);
747}
748
749module_init(tce_iommu_init);
750module_exit(tce_iommu_cleanup);
751
752MODULE_VERSION(DRIVER_VERSION);
753MODULE_LICENSE("GPL v2");
754MODULE_AUTHOR(DRIVER_AUTHOR);
755MODULE_DESCRIPTION(DRIVER_DESC);
756