2 * VFIO: IOMMU DMA mapping support for TCE on POWER
4 * Copyright (C) 2013 IBM Corp. All rights reserved.
5 * Author: Alexey Kardashevskiy <aik@ozlabs.ru>
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.
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>
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 <linux/vmalloc.h>
23 #include <linux/sched/mm.h>
24 #include <linux/sched/signal.h>
26 #include <asm/iommu.h>
28 #include <asm/mmu_context.h>
30 #define DRIVER_VERSION "0.1"
31 #define DRIVER_AUTHOR "aik@ozlabs.ru"
32 #define DRIVER_DESC "VFIO IOMMU SPAPR TCE"
34 static void tce_iommu_detach_group(void *iommu_data
,
35 struct iommu_group
*iommu_group
);
37 static long try_increment_locked_vm(struct mm_struct
*mm
, long npages
)
39 long ret
= 0, locked
, lock_limit
;
41 if (WARN_ON_ONCE(!mm
))
47 down_write(&mm
->mmap_sem
);
48 locked
= mm
->locked_vm
+ npages
;
49 lock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
50 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
53 mm
->locked_vm
+= npages
;
55 pr_debug("[%d] RLIMIT_MEMLOCK +%ld %ld/%ld%s\n", current
->pid
,
57 mm
->locked_vm
<< PAGE_SHIFT
,
58 rlimit(RLIMIT_MEMLOCK
),
59 ret
? " - exceeded" : "");
61 up_write(&mm
->mmap_sem
);
66 static void decrement_locked_vm(struct mm_struct
*mm
, long npages
)
71 down_write(&mm
->mmap_sem
);
72 if (WARN_ON_ONCE(npages
> mm
->locked_vm
))
73 npages
= mm
->locked_vm
;
74 mm
->locked_vm
-= npages
;
75 pr_debug("[%d] RLIMIT_MEMLOCK -%ld %ld/%ld\n", current
->pid
,
77 mm
->locked_vm
<< PAGE_SHIFT
,
78 rlimit(RLIMIT_MEMLOCK
));
79 up_write(&mm
->mmap_sem
);
83 * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
85 * This code handles mapping and unmapping of user data buffers
86 * into DMA'ble space using the IOMMU
89 struct tce_iommu_group
{
90 struct list_head next
;
91 struct iommu_group
*grp
;
95 * A container needs to remember which preregistered region it has
96 * referenced to do proper cleanup at the userspace process exit.
98 struct tce_iommu_prereg
{
99 struct list_head next
;
100 struct mm_iommu_table_group_mem_t
*mem
;
104 * The container descriptor supports only a single group per container.
105 * Required by the API as the container is not supplied with the IOMMU group
106 * at the moment of initialization.
108 struct tce_container
{
112 bool def_window_pending
;
113 unsigned long locked_pages
;
114 struct mm_struct
*mm
;
115 struct iommu_table
*tables
[IOMMU_TABLE_GROUP_MAX_TABLES
];
116 struct list_head group_list
;
117 struct list_head prereg_list
;
120 static long tce_iommu_mm_set(struct tce_container
*container
)
123 if (container
->mm
== current
->mm
)
127 BUG_ON(!current
->mm
);
128 container
->mm
= current
->mm
;
129 atomic_inc(&container
->mm
->mm_count
);
134 static long tce_iommu_prereg_free(struct tce_container
*container
,
135 struct tce_iommu_prereg
*tcemem
)
139 ret
= mm_iommu_put(container
->mm
, tcemem
->mem
);
143 list_del(&tcemem
->next
);
149 static long tce_iommu_unregister_pages(struct tce_container
*container
,
150 __u64 vaddr
, __u64 size
)
152 struct mm_iommu_table_group_mem_t
*mem
;
153 struct tce_iommu_prereg
*tcemem
;
156 if ((vaddr
& ~PAGE_MASK
) || (size
& ~PAGE_MASK
))
159 mem
= mm_iommu_find(container
->mm
, vaddr
, size
>> PAGE_SHIFT
);
163 list_for_each_entry(tcemem
, &container
->prereg_list
, next
) {
164 if (tcemem
->mem
== mem
) {
173 return tce_iommu_prereg_free(container
, tcemem
);
176 static long tce_iommu_register_pages(struct tce_container
*container
,
177 __u64 vaddr
, __u64 size
)
180 struct mm_iommu_table_group_mem_t
*mem
= NULL
;
181 struct tce_iommu_prereg
*tcemem
;
182 unsigned long entries
= size
>> PAGE_SHIFT
;
184 if ((vaddr
& ~PAGE_MASK
) || (size
& ~PAGE_MASK
) ||
185 ((vaddr
+ size
) < vaddr
))
188 mem
= mm_iommu_find(container
->mm
, vaddr
, entries
);
190 list_for_each_entry(tcemem
, &container
->prereg_list
, next
) {
191 if (tcemem
->mem
== mem
)
196 ret
= mm_iommu_get(container
->mm
, vaddr
, entries
, &mem
);
200 tcemem
= kzalloc(sizeof(*tcemem
), GFP_KERNEL
);
202 list_add(&tcemem
->next
, &container
->prereg_list
);
204 container
->enabled
= true;
209 static long tce_iommu_userspace_view_alloc(struct iommu_table
*tbl
,
210 struct mm_struct
*mm
)
212 unsigned long cb
= _ALIGN_UP(sizeof(tbl
->it_userspace
[0]) *
213 tbl
->it_size
, PAGE_SIZE
);
217 BUG_ON(tbl
->it_userspace
);
219 ret
= try_increment_locked_vm(mm
, cb
>> PAGE_SHIFT
);
225 decrement_locked_vm(mm
, cb
>> PAGE_SHIFT
);
228 tbl
->it_userspace
= uas
;
233 static void tce_iommu_userspace_view_free(struct iommu_table
*tbl
,
234 struct mm_struct
*mm
)
236 unsigned long cb
= _ALIGN_UP(sizeof(tbl
->it_userspace
[0]) *
237 tbl
->it_size
, PAGE_SIZE
);
239 if (!tbl
->it_userspace
)
242 vfree(tbl
->it_userspace
);
243 tbl
->it_userspace
= NULL
;
244 decrement_locked_vm(mm
, cb
>> PAGE_SHIFT
);
247 static bool tce_page_is_contained(struct page
*page
, unsigned page_shift
)
250 * Check that the TCE table granularity is not bigger than the size of
251 * a page we just found. Otherwise the hardware can get access to
252 * a bigger memory chunk that it should.
254 return (PAGE_SHIFT
+ compound_order(compound_head(page
))) >= page_shift
;
257 static inline bool tce_groups_attached(struct tce_container
*container
)
259 return !list_empty(&container
->group_list
);
262 static long tce_iommu_find_table(struct tce_container
*container
,
263 phys_addr_t ioba
, struct iommu_table
**ptbl
)
267 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
268 struct iommu_table
*tbl
= container
->tables
[i
];
271 unsigned long entry
= ioba
>> tbl
->it_page_shift
;
272 unsigned long start
= tbl
->it_offset
;
273 unsigned long end
= start
+ tbl
->it_size
;
275 if ((start
<= entry
) && (entry
< end
)) {
285 static int tce_iommu_find_free_table(struct tce_container
*container
)
289 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
290 if (!container
->tables
[i
])
297 static int tce_iommu_enable(struct tce_container
*container
)
300 unsigned long locked
;
301 struct iommu_table_group
*table_group
;
302 struct tce_iommu_group
*tcegrp
;
304 if (container
->enabled
)
308 * When userspace pages are mapped into the IOMMU, they are effectively
309 * locked memory, so, theoretically, we need to update the accounting
310 * of locked pages on each map and unmap. For powerpc, the map unmap
311 * paths can be very hot, though, and the accounting would kill
312 * performance, especially since it would be difficult to impossible
313 * to handle the accounting in real mode only.
315 * To address that, rather than precisely accounting every page, we
316 * instead account for a worst case on locked memory when the iommu is
317 * enabled and disabled. The worst case upper bound on locked memory
318 * is the size of the whole iommu window, which is usually relatively
319 * small (compared to total memory sizes) on POWER hardware.
321 * Also we don't have a nice way to fail on H_PUT_TCE due to ulimits,
322 * that would effectively kill the guest at random points, much better
323 * enforcing the limit based on the max that the guest can map.
325 * Unfortunately at the moment it counts whole tables, no matter how
326 * much memory the guest has. I.e. for 4GB guest and 4 IOMMU groups
327 * each with 2GB DMA window, 8GB will be counted here. The reason for
328 * this is that we cannot tell here the amount of RAM used by the guest
329 * as this information is only available from KVM and VFIO is
332 * So we do not allow enabling a container without a group attached
333 * as there is no way to know how much we should increment
334 * the locked_vm counter.
336 if (!tce_groups_attached(container
))
339 tcegrp
= list_first_entry(&container
->group_list
,
340 struct tce_iommu_group
, next
);
341 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
345 if (!table_group
->tce32_size
)
348 ret
= tce_iommu_mm_set(container
);
352 locked
= table_group
->tce32_size
>> PAGE_SHIFT
;
353 ret
= try_increment_locked_vm(container
->mm
, locked
);
357 container
->locked_pages
= locked
;
359 container
->enabled
= true;
364 static void tce_iommu_disable(struct tce_container
*container
)
366 if (!container
->enabled
)
369 container
->enabled
= false;
371 BUG_ON(!container
->mm
);
372 decrement_locked_vm(container
->mm
, container
->locked_pages
);
375 static void *tce_iommu_open(unsigned long arg
)
377 struct tce_container
*container
;
379 if ((arg
!= VFIO_SPAPR_TCE_IOMMU
) && (arg
!= VFIO_SPAPR_TCE_v2_IOMMU
)) {
380 pr_err("tce_vfio: Wrong IOMMU type\n");
381 return ERR_PTR(-EINVAL
);
384 container
= kzalloc(sizeof(*container
), GFP_KERNEL
);
386 return ERR_PTR(-ENOMEM
);
388 mutex_init(&container
->lock
);
389 INIT_LIST_HEAD_RCU(&container
->group_list
);
390 INIT_LIST_HEAD_RCU(&container
->prereg_list
);
392 container
->v2
= arg
== VFIO_SPAPR_TCE_v2_IOMMU
;
397 static int tce_iommu_clear(struct tce_container
*container
,
398 struct iommu_table
*tbl
,
399 unsigned long entry
, unsigned long pages
);
400 static void tce_iommu_free_table(struct tce_container
*container
,
401 struct iommu_table
*tbl
);
403 static void tce_iommu_release(void *iommu_data
)
405 struct tce_container
*container
= iommu_data
;
406 struct tce_iommu_group
*tcegrp
;
409 while (tce_groups_attached(container
)) {
410 tcegrp
= list_first_entry(&container
->group_list
,
411 struct tce_iommu_group
, next
);
412 tce_iommu_detach_group(iommu_data
, tcegrp
->grp
);
416 * If VFIO created a table, it was not disposed
417 * by tce_iommu_detach_group() so do it now.
419 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
420 struct iommu_table
*tbl
= container
->tables
[i
];
425 tce_iommu_clear(container
, tbl
, tbl
->it_offset
, tbl
->it_size
);
426 tce_iommu_free_table(container
, tbl
);
429 while (!list_empty(&container
->prereg_list
)) {
430 struct tce_iommu_prereg
*tcemem
;
432 tcemem
= list_first_entry(&container
->prereg_list
,
433 struct tce_iommu_prereg
, next
);
434 WARN_ON_ONCE(tce_iommu_prereg_free(container
, tcemem
));
437 tce_iommu_disable(container
);
439 mmdrop(container
->mm
);
440 mutex_destroy(&container
->lock
);
445 static void tce_iommu_unuse_page(struct tce_container
*container
,
450 page
= pfn_to_page(hpa
>> PAGE_SHIFT
);
454 static int tce_iommu_prereg_ua_to_hpa(struct tce_container
*container
,
455 unsigned long tce
, unsigned long size
,
456 unsigned long *phpa
, struct mm_iommu_table_group_mem_t
**pmem
)
459 struct mm_iommu_table_group_mem_t
*mem
;
461 mem
= mm_iommu_lookup(container
->mm
, tce
, size
);
465 ret
= mm_iommu_ua_to_hpa(mem
, tce
, phpa
);
474 static void tce_iommu_unuse_page_v2(struct tce_container
*container
,
475 struct iommu_table
*tbl
, unsigned long entry
)
477 struct mm_iommu_table_group_mem_t
*mem
= NULL
;
479 unsigned long hpa
= 0;
480 unsigned long *pua
= IOMMU_TABLE_USERSPACE_ENTRY(tbl
, entry
);
485 ret
= tce_iommu_prereg_ua_to_hpa(container
, *pua
, IOMMU_PAGE_SIZE(tbl
),
488 pr_debug("%s: tce %lx at #%lx was not cached, ret=%d\n",
489 __func__
, *pua
, entry
, ret
);
491 mm_iommu_mapped_dec(mem
);
496 static int tce_iommu_clear(struct tce_container
*container
,
497 struct iommu_table
*tbl
,
498 unsigned long entry
, unsigned long pages
)
500 unsigned long oldhpa
;
502 enum dma_data_direction direction
;
504 for ( ; pages
; --pages
, ++entry
) {
505 direction
= DMA_NONE
;
507 ret
= iommu_tce_xchg(tbl
, entry
, &oldhpa
, &direction
);
511 if (direction
== DMA_NONE
)
515 tce_iommu_unuse_page_v2(container
, tbl
, entry
);
519 tce_iommu_unuse_page(container
, oldhpa
);
525 static int tce_iommu_use_page(unsigned long tce
, unsigned long *hpa
)
527 struct page
*page
= NULL
;
528 enum dma_data_direction direction
= iommu_tce_direction(tce
);
530 if (get_user_pages_fast(tce
& PAGE_MASK
, 1,
531 direction
!= DMA_TO_DEVICE
, &page
) != 1)
534 *hpa
= __pa((unsigned long) page_address(page
));
539 static long tce_iommu_build(struct tce_container
*container
,
540 struct iommu_table
*tbl
,
541 unsigned long entry
, unsigned long tce
, unsigned long pages
,
542 enum dma_data_direction direction
)
547 enum dma_data_direction dirtmp
;
549 for (i
= 0; i
< pages
; ++i
) {
550 unsigned long offset
= tce
& IOMMU_PAGE_MASK(tbl
) & ~PAGE_MASK
;
552 ret
= tce_iommu_use_page(tce
, &hpa
);
556 page
= pfn_to_page(hpa
>> PAGE_SHIFT
);
557 if (!tce_page_is_contained(page
, tbl
->it_page_shift
)) {
564 ret
= iommu_tce_xchg(tbl
, entry
+ i
, &hpa
, &dirtmp
);
566 tce_iommu_unuse_page(container
, hpa
);
567 pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
568 __func__
, entry
<< tbl
->it_page_shift
,
573 if (dirtmp
!= DMA_NONE
)
574 tce_iommu_unuse_page(container
, hpa
);
576 tce
+= IOMMU_PAGE_SIZE(tbl
);
580 tce_iommu_clear(container
, tbl
, entry
, i
);
585 static long tce_iommu_build_v2(struct tce_container
*container
,
586 struct iommu_table
*tbl
,
587 unsigned long entry
, unsigned long tce
, unsigned long pages
,
588 enum dma_data_direction direction
)
593 enum dma_data_direction dirtmp
;
595 if (!tbl
->it_userspace
) {
596 ret
= tce_iommu_userspace_view_alloc(tbl
, container
->mm
);
601 for (i
= 0; i
< pages
; ++i
) {
602 struct mm_iommu_table_group_mem_t
*mem
= NULL
;
603 unsigned long *pua
= IOMMU_TABLE_USERSPACE_ENTRY(tbl
,
606 ret
= tce_iommu_prereg_ua_to_hpa(container
,
607 tce
, IOMMU_PAGE_SIZE(tbl
), &hpa
, &mem
);
611 page
= pfn_to_page(hpa
>> PAGE_SHIFT
);
612 if (!tce_page_is_contained(page
, tbl
->it_page_shift
)) {
617 /* Preserve offset within IOMMU page */
618 hpa
|= tce
& IOMMU_PAGE_MASK(tbl
) & ~PAGE_MASK
;
621 /* The registered region is being unregistered */
622 if (mm_iommu_mapped_inc(mem
))
625 ret
= iommu_tce_xchg(tbl
, entry
+ i
, &hpa
, &dirtmp
);
627 /* dirtmp cannot be DMA_NONE here */
628 tce_iommu_unuse_page_v2(container
, tbl
, entry
+ i
);
629 pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
630 __func__
, entry
<< tbl
->it_page_shift
,
635 if (dirtmp
!= DMA_NONE
)
636 tce_iommu_unuse_page_v2(container
, tbl
, entry
+ i
);
640 tce
+= IOMMU_PAGE_SIZE(tbl
);
644 tce_iommu_clear(container
, tbl
, entry
, i
);
649 static long tce_iommu_create_table(struct tce_container
*container
,
650 struct iommu_table_group
*table_group
,
655 struct iommu_table
**ptbl
)
657 long ret
, table_size
;
659 table_size
= table_group
->ops
->get_table_size(page_shift
, window_size
,
664 ret
= try_increment_locked_vm(container
->mm
, table_size
>> PAGE_SHIFT
);
668 ret
= table_group
->ops
->create_table(table_group
, num
,
669 page_shift
, window_size
, levels
, ptbl
);
671 WARN_ON(!ret
&& !(*ptbl
)->it_ops
->free
);
672 WARN_ON(!ret
&& ((*ptbl
)->it_allocated_size
!= table_size
));
677 static void tce_iommu_free_table(struct tce_container
*container
,
678 struct iommu_table
*tbl
)
680 unsigned long pages
= tbl
->it_allocated_size
>> PAGE_SHIFT
;
682 tce_iommu_userspace_view_free(tbl
, container
->mm
);
683 tbl
->it_ops
->free(tbl
);
684 decrement_locked_vm(container
->mm
, pages
);
687 static long tce_iommu_create_window(struct tce_container
*container
,
688 __u32 page_shift
, __u64 window_size
, __u32 levels
,
691 struct tce_iommu_group
*tcegrp
;
692 struct iommu_table_group
*table_group
;
693 struct iommu_table
*tbl
= NULL
;
696 num
= tce_iommu_find_free_table(container
);
700 /* Get the first group for ops::create_table */
701 tcegrp
= list_first_entry(&container
->group_list
,
702 struct tce_iommu_group
, next
);
703 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
707 if (!(table_group
->pgsizes
& (1ULL << page_shift
)))
710 if (!table_group
->ops
->set_window
|| !table_group
->ops
->unset_window
||
711 !table_group
->ops
->get_table_size
||
712 !table_group
->ops
->create_table
)
715 /* Create TCE table */
716 ret
= tce_iommu_create_table(container
, table_group
, num
,
717 page_shift
, window_size
, levels
, &tbl
);
721 BUG_ON(!tbl
->it_ops
->free
);
724 * Program the table to every group.
725 * Groups have been tested for compatibility at the attach time.
727 list_for_each_entry(tcegrp
, &container
->group_list
, next
) {
728 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
730 ret
= table_group
->ops
->set_window(table_group
, num
, tbl
);
735 container
->tables
[num
] = tbl
;
737 /* Return start address assigned by platform in create_table() */
738 *start_addr
= tbl
->it_offset
<< tbl
->it_page_shift
;
743 list_for_each_entry(tcegrp
, &container
->group_list
, next
) {
744 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
745 table_group
->ops
->unset_window(table_group
, num
);
747 tce_iommu_free_table(container
, tbl
);
752 static long tce_iommu_remove_window(struct tce_container
*container
,
755 struct iommu_table_group
*table_group
= NULL
;
756 struct iommu_table
*tbl
;
757 struct tce_iommu_group
*tcegrp
;
760 num
= tce_iommu_find_table(container
, start_addr
, &tbl
);
764 BUG_ON(!tbl
->it_size
);
766 /* Detach groups from IOMMUs */
767 list_for_each_entry(tcegrp
, &container
->group_list
, next
) {
768 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
771 * SPAPR TCE IOMMU exposes the default DMA window to
772 * the guest via dma32_window_start/size of
773 * VFIO_IOMMU_SPAPR_TCE_GET_INFO. Some platforms allow
774 * the userspace to remove this window, some do not so
775 * here we check for the platform capability.
777 if (!table_group
->ops
|| !table_group
->ops
->unset_window
)
780 table_group
->ops
->unset_window(table_group
, num
);
784 tce_iommu_clear(container
, tbl
, tbl
->it_offset
, tbl
->it_size
);
785 tce_iommu_free_table(container
, tbl
);
786 container
->tables
[num
] = NULL
;
791 static long tce_iommu_create_default_window(struct tce_container
*container
)
794 __u64 start_addr
= 0;
795 struct tce_iommu_group
*tcegrp
;
796 struct iommu_table_group
*table_group
;
798 if (!container
->def_window_pending
)
801 if (!tce_groups_attached(container
))
804 tcegrp
= list_first_entry(&container
->group_list
,
805 struct tce_iommu_group
, next
);
806 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
810 ret
= tce_iommu_create_window(container
, IOMMU_PAGE_SHIFT_4K
,
811 table_group
->tce32_size
, 1, &start_addr
);
812 WARN_ON_ONCE(!ret
&& start_addr
);
815 container
->def_window_pending
= false;
820 static long tce_iommu_ioctl(void *iommu_data
,
821 unsigned int cmd
, unsigned long arg
)
823 struct tce_container
*container
= iommu_data
;
824 unsigned long minsz
, ddwsz
;
828 case VFIO_CHECK_EXTENSION
:
830 case VFIO_SPAPR_TCE_IOMMU
:
831 case VFIO_SPAPR_TCE_v2_IOMMU
:
835 ret
= vfio_spapr_iommu_eeh_ioctl(NULL
, cmd
, arg
);
839 return (ret
< 0) ? 0 : ret
;
843 * Sanity check to prevent one userspace from manipulating
844 * another userspace mm.
847 if (container
->mm
&& container
->mm
!= current
->mm
)
851 case VFIO_IOMMU_SPAPR_TCE_GET_INFO
: {
852 struct vfio_iommu_spapr_tce_info info
;
853 struct tce_iommu_group
*tcegrp
;
854 struct iommu_table_group
*table_group
;
856 if (!tce_groups_attached(container
))
859 tcegrp
= list_first_entry(&container
->group_list
,
860 struct tce_iommu_group
, next
);
861 table_group
= iommu_group_get_iommudata(tcegrp
->grp
);
866 minsz
= offsetofend(struct vfio_iommu_spapr_tce_info
,
869 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
872 if (info
.argsz
< minsz
)
875 info
.dma32_window_start
= table_group
->tce32_start
;
876 info
.dma32_window_size
= table_group
->tce32_size
;
878 memset(&info
.ddw
, 0, sizeof(info
.ddw
));
880 if (table_group
->max_dynamic_windows_supported
&&
882 info
.flags
|= VFIO_IOMMU_SPAPR_INFO_DDW
;
883 info
.ddw
.pgsizes
= table_group
->pgsizes
;
884 info
.ddw
.max_dynamic_windows_supported
=
885 table_group
->max_dynamic_windows_supported
;
886 info
.ddw
.levels
= table_group
->max_levels
;
889 ddwsz
= offsetofend(struct vfio_iommu_spapr_tce_info
, ddw
);
891 if (info
.argsz
>= ddwsz
)
894 if (copy_to_user((void __user
*)arg
, &info
, minsz
))
899 case VFIO_IOMMU_MAP_DMA
: {
900 struct vfio_iommu_type1_dma_map param
;
901 struct iommu_table
*tbl
= NULL
;
903 enum dma_data_direction direction
;
905 if (!container
->enabled
)
908 minsz
= offsetofend(struct vfio_iommu_type1_dma_map
, size
);
910 if (copy_from_user(¶m
, (void __user
*)arg
, minsz
))
913 if (param
.argsz
< minsz
)
916 if (param
.flags
& ~(VFIO_DMA_MAP_FLAG_READ
|
917 VFIO_DMA_MAP_FLAG_WRITE
))
920 ret
= tce_iommu_create_default_window(container
);
924 num
= tce_iommu_find_table(container
, param
.iova
, &tbl
);
928 if ((param
.size
& ~IOMMU_PAGE_MASK(tbl
)) ||
929 (param
.vaddr
& ~IOMMU_PAGE_MASK(tbl
)))
932 /* iova is checked by the IOMMU API */
933 if (param
.flags
& VFIO_DMA_MAP_FLAG_READ
) {
934 if (param
.flags
& VFIO_DMA_MAP_FLAG_WRITE
)
935 direction
= DMA_BIDIRECTIONAL
;
937 direction
= DMA_TO_DEVICE
;
939 if (param
.flags
& VFIO_DMA_MAP_FLAG_WRITE
)
940 direction
= DMA_FROM_DEVICE
;
945 ret
= iommu_tce_put_param_check(tbl
, param
.iova
, param
.vaddr
);
950 ret
= tce_iommu_build_v2(container
, tbl
,
951 param
.iova
>> tbl
->it_page_shift
,
953 param
.size
>> tbl
->it_page_shift
,
956 ret
= tce_iommu_build(container
, tbl
,
957 param
.iova
>> tbl
->it_page_shift
,
959 param
.size
>> tbl
->it_page_shift
,
962 iommu_flush_tce(tbl
);
966 case VFIO_IOMMU_UNMAP_DMA
: {
967 struct vfio_iommu_type1_dma_unmap param
;
968 struct iommu_table
*tbl
= NULL
;
971 if (!container
->enabled
)
974 minsz
= offsetofend(struct vfio_iommu_type1_dma_unmap
,
977 if (copy_from_user(¶m
, (void __user
*)arg
, minsz
))
980 if (param
.argsz
< minsz
)
983 /* No flag is supported now */
987 ret
= tce_iommu_create_default_window(container
);
991 num
= tce_iommu_find_table(container
, param
.iova
, &tbl
);
995 if (param
.size
& ~IOMMU_PAGE_MASK(tbl
))
998 ret
= iommu_tce_clear_param_check(tbl
, param
.iova
, 0,
999 param
.size
>> tbl
->it_page_shift
);
1003 ret
= tce_iommu_clear(container
, tbl
,
1004 param
.iova
>> tbl
->it_page_shift
,
1005 param
.size
>> tbl
->it_page_shift
);
1006 iommu_flush_tce(tbl
);
1010 case VFIO_IOMMU_SPAPR_REGISTER_MEMORY
: {
1011 struct vfio_iommu_spapr_register_memory param
;
1016 minsz
= offsetofend(struct vfio_iommu_spapr_register_memory
,
1019 ret
= tce_iommu_mm_set(container
);
1023 if (copy_from_user(¶m
, (void __user
*)arg
, minsz
))
1026 if (param
.argsz
< minsz
)
1029 /* No flag is supported now */
1033 mutex_lock(&container
->lock
);
1034 ret
= tce_iommu_register_pages(container
, param
.vaddr
,
1036 mutex_unlock(&container
->lock
);
1040 case VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY
: {
1041 struct vfio_iommu_spapr_register_memory param
;
1049 minsz
= offsetofend(struct vfio_iommu_spapr_register_memory
,
1052 if (copy_from_user(¶m
, (void __user
*)arg
, minsz
))
1055 if (param
.argsz
< minsz
)
1058 /* No flag is supported now */
1062 mutex_lock(&container
->lock
);
1063 ret
= tce_iommu_unregister_pages(container
, param
.vaddr
,
1065 mutex_unlock(&container
->lock
);
1069 case VFIO_IOMMU_ENABLE
:
1073 mutex_lock(&container
->lock
);
1074 ret
= tce_iommu_enable(container
);
1075 mutex_unlock(&container
->lock
);
1079 case VFIO_IOMMU_DISABLE
:
1083 mutex_lock(&container
->lock
);
1084 tce_iommu_disable(container
);
1085 mutex_unlock(&container
->lock
);
1088 case VFIO_EEH_PE_OP
: {
1089 struct tce_iommu_group
*tcegrp
;
1092 list_for_each_entry(tcegrp
, &container
->group_list
, next
) {
1093 ret
= vfio_spapr_iommu_eeh_ioctl(tcegrp
->grp
,
1101 case VFIO_IOMMU_SPAPR_TCE_CREATE
: {
1102 struct vfio_iommu_spapr_tce_create create
;
1107 ret
= tce_iommu_mm_set(container
);
1111 if (!tce_groups_attached(container
))
1114 minsz
= offsetofend(struct vfio_iommu_spapr_tce_create
,
1117 if (copy_from_user(&create
, (void __user
*)arg
, minsz
))
1120 if (create
.argsz
< minsz
)
1126 mutex_lock(&container
->lock
);
1128 ret
= tce_iommu_create_default_window(container
);
1130 ret
= tce_iommu_create_window(container
,
1132 create
.window_size
, create
.levels
,
1133 &create
.start_addr
);
1135 mutex_unlock(&container
->lock
);
1137 if (!ret
&& copy_to_user((void __user
*)arg
, &create
, minsz
))
1142 case VFIO_IOMMU_SPAPR_TCE_REMOVE
: {
1143 struct vfio_iommu_spapr_tce_remove remove
;
1148 ret
= tce_iommu_mm_set(container
);
1152 if (!tce_groups_attached(container
))
1155 minsz
= offsetofend(struct vfio_iommu_spapr_tce_remove
,
1158 if (copy_from_user(&remove
, (void __user
*)arg
, minsz
))
1161 if (remove
.argsz
< minsz
)
1167 if (container
->def_window_pending
&& !remove
.start_addr
) {
1168 container
->def_window_pending
= false;
1172 mutex_lock(&container
->lock
);
1174 ret
= tce_iommu_remove_window(container
, remove
.start_addr
);
1176 mutex_unlock(&container
->lock
);
1185 static void tce_iommu_release_ownership(struct tce_container
*container
,
1186 struct iommu_table_group
*table_group
)
1190 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
1191 struct iommu_table
*tbl
= container
->tables
[i
];
1196 tce_iommu_clear(container
, tbl
, tbl
->it_offset
, tbl
->it_size
);
1197 tce_iommu_userspace_view_free(tbl
, container
->mm
);
1199 iommu_release_ownership(tbl
);
1201 container
->tables
[i
] = NULL
;
1205 static int tce_iommu_take_ownership(struct tce_container
*container
,
1206 struct iommu_table_group
*table_group
)
1210 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
1211 struct iommu_table
*tbl
= table_group
->tables
[i
];
1213 if (!tbl
|| !tbl
->it_map
)
1216 rc
= iommu_take_ownership(tbl
);
1218 for (j
= 0; j
< i
; ++j
)
1219 iommu_release_ownership(
1220 table_group
->tables
[j
]);
1226 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
)
1227 container
->tables
[i
] = table_group
->tables
[i
];
1232 static void tce_iommu_release_ownership_ddw(struct tce_container
*container
,
1233 struct iommu_table_group
*table_group
)
1237 if (!table_group
->ops
->unset_window
) {
1242 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
)
1243 table_group
->ops
->unset_window(table_group
, i
);
1245 table_group
->ops
->release_ownership(table_group
);
1248 static long tce_iommu_take_ownership_ddw(struct tce_container
*container
,
1249 struct iommu_table_group
*table_group
)
1253 if (!table_group
->ops
->create_table
|| !table_group
->ops
->set_window
||
1254 !table_group
->ops
->release_ownership
) {
1259 table_group
->ops
->take_ownership(table_group
);
1261 /* Set all windows to the new group */
1262 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
) {
1263 struct iommu_table
*tbl
= container
->tables
[i
];
1268 ret
= table_group
->ops
->set_window(table_group
, i
, tbl
);
1276 for (i
= 0; i
< IOMMU_TABLE_GROUP_MAX_TABLES
; ++i
)
1277 table_group
->ops
->unset_window(table_group
, i
);
1279 table_group
->ops
->release_ownership(table_group
);
1284 static int tce_iommu_attach_group(void *iommu_data
,
1285 struct iommu_group
*iommu_group
)
1288 struct tce_container
*container
= iommu_data
;
1289 struct iommu_table_group
*table_group
;
1290 struct tce_iommu_group
*tcegrp
= NULL
;
1292 mutex_lock(&container
->lock
);
1294 /* pr_debug("tce_vfio: Attaching group #%u to iommu %p\n",
1295 iommu_group_id(iommu_group), iommu_group); */
1296 table_group
= iommu_group_get_iommudata(iommu_group
);
1302 if (tce_groups_attached(container
) && (!table_group
->ops
||
1303 !table_group
->ops
->take_ownership
||
1304 !table_group
->ops
->release_ownership
)) {
1309 /* Check if new group has the same iommu_ops (i.e. compatible) */
1310 list_for_each_entry(tcegrp
, &container
->group_list
, next
) {
1311 struct iommu_table_group
*table_group_tmp
;
1313 if (tcegrp
->grp
== iommu_group
) {
1314 pr_warn("tce_vfio: Group %d is already attached\n",
1315 iommu_group_id(iommu_group
));
1319 table_group_tmp
= iommu_group_get_iommudata(tcegrp
->grp
);
1320 if (table_group_tmp
->ops
->create_table
!=
1321 table_group
->ops
->create_table
) {
1322 pr_warn("tce_vfio: Group %d is incompatible with group %d\n",
1323 iommu_group_id(iommu_group
),
1324 iommu_group_id(tcegrp
->grp
));
1330 tcegrp
= kzalloc(sizeof(*tcegrp
), GFP_KERNEL
);
1336 if (!table_group
->ops
|| !table_group
->ops
->take_ownership
||
1337 !table_group
->ops
->release_ownership
) {
1338 ret
= tce_iommu_take_ownership(container
, table_group
);
1340 ret
= tce_iommu_take_ownership_ddw(container
, table_group
);
1341 if (!tce_groups_attached(container
) && !container
->tables
[0])
1342 container
->def_window_pending
= true;
1346 tcegrp
->grp
= iommu_group
;
1347 list_add(&tcegrp
->next
, &container
->group_list
);
1354 mutex_unlock(&container
->lock
);
1359 static void tce_iommu_detach_group(void *iommu_data
,
1360 struct iommu_group
*iommu_group
)
1362 struct tce_container
*container
= iommu_data
;
1363 struct iommu_table_group
*table_group
;
1365 struct tce_iommu_group
*tcegrp
;
1367 mutex_lock(&container
->lock
);
1369 list_for_each_entry(tcegrp
, &container
->group_list
, next
) {
1370 if (tcegrp
->grp
== iommu_group
) {
1377 pr_warn("tce_vfio: detaching unattached group #%u\n",
1378 iommu_group_id(iommu_group
));
1382 list_del(&tcegrp
->next
);
1385 table_group
= iommu_group_get_iommudata(iommu_group
);
1386 BUG_ON(!table_group
);
1388 if (!table_group
->ops
|| !table_group
->ops
->release_ownership
)
1389 tce_iommu_release_ownership(container
, table_group
);
1391 tce_iommu_release_ownership_ddw(container
, table_group
);
1394 mutex_unlock(&container
->lock
);
1397 const struct vfio_iommu_driver_ops tce_iommu_driver_ops
= {
1398 .name
= "iommu-vfio-powerpc",
1399 .owner
= THIS_MODULE
,
1400 .open
= tce_iommu_open
,
1401 .release
= tce_iommu_release
,
1402 .ioctl
= tce_iommu_ioctl
,
1403 .attach_group
= tce_iommu_attach_group
,
1404 .detach_group
= tce_iommu_detach_group
,
1407 static int __init
tce_iommu_init(void)
1409 return vfio_register_iommu_driver(&tce_iommu_driver_ops
);
1412 static void __exit
tce_iommu_cleanup(void)
1414 vfio_unregister_iommu_driver(&tce_iommu_driver_ops
);
1417 module_init(tce_iommu_init
);
1418 module_exit(tce_iommu_cleanup
);
1420 MODULE_VERSION(DRIVER_VERSION
);
1421 MODULE_LICENSE("GPL v2");
1422 MODULE_AUTHOR(DRIVER_AUTHOR
);
1423 MODULE_DESCRIPTION(DRIVER_DESC
);