xref: /openbmc/qemu/hw/vfio/spapr.c (revision a17879f0e2e82c5e85440eb1c3e8a3eeef469a3e)
1318f67ceSAlexey Kardashevskiy /*
2318f67ceSAlexey Kardashevskiy  * DMA memory preregistration
3318f67ceSAlexey Kardashevskiy  *
4318f67ceSAlexey Kardashevskiy  * Authors:
5318f67ceSAlexey Kardashevskiy  *  Alexey Kardashevskiy <aik@ozlabs.ru>
6318f67ceSAlexey Kardashevskiy  *
7318f67ceSAlexey Kardashevskiy  * This work is licensed under the terms of the GNU GPL, version 2.  See
8318f67ceSAlexey Kardashevskiy  * the COPYING file in the top-level directory.
9318f67ceSAlexey Kardashevskiy  */
10318f67ceSAlexey Kardashevskiy 
11318f67ceSAlexey Kardashevskiy #include "qemu/osdep.h"
12318f67ceSAlexey Kardashevskiy #include <sys/ioctl.h>
13318f67ceSAlexey Kardashevskiy #include <linux/vfio.h>
14521c8f4eSZhenzhong Duan #ifdef CONFIG_KVM
15521c8f4eSZhenzhong Duan #include <linux/kvm.h>
16521c8f4eSZhenzhong Duan #endif
17521c8f4eSZhenzhong Duan #include "sysemu/kvm.h"
18770c3b6eSZhenzhong Duan #include "exec/address-spaces.h"
19318f67ceSAlexey Kardashevskiy 
20318f67ceSAlexey Kardashevskiy #include "hw/vfio/vfio-common.h"
21318f67ceSAlexey Kardashevskiy #include "hw/hw.h"
22c26bc185SAlexey Kardashevskiy #include "exec/ram_addr.h"
23318f67ceSAlexey Kardashevskiy #include "qemu/error-report.h"
24d7d87836SEric Auger #include "qapi/error.h"
25318f67ceSAlexey Kardashevskiy #include "trace.h"
26318f67ceSAlexey Kardashevskiy 
27318f67ceSAlexey Kardashevskiy static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
28318f67ceSAlexey Kardashevskiy {
29318f67ceSAlexey Kardashevskiy     if (memory_region_is_iommu(section->mr)) {
30318f67ceSAlexey Kardashevskiy         hw_error("Cannot possibly preregister IOMMU memory");
31318f67ceSAlexey Kardashevskiy     }
32318f67ceSAlexey Kardashevskiy 
33318f67ceSAlexey Kardashevskiy     return !memory_region_is_ram(section->mr) ||
3421e00fa5SAlex Williamson             memory_region_is_ram_device(section->mr);
35318f67ceSAlexey Kardashevskiy }
36318f67ceSAlexey Kardashevskiy 
37318f67ceSAlexey Kardashevskiy static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa)
38318f67ceSAlexey Kardashevskiy {
39318f67ceSAlexey Kardashevskiy     return memory_region_get_ram_ptr(section->mr) +
40318f67ceSAlexey Kardashevskiy         section->offset_within_region +
41318f67ceSAlexey Kardashevskiy         (gpa - section->offset_within_address_space);
42318f67ceSAlexey Kardashevskiy }
43318f67ceSAlexey Kardashevskiy 
44318f67ceSAlexey Kardashevskiy static void vfio_prereg_listener_region_add(MemoryListener *listener,
45318f67ceSAlexey Kardashevskiy                                             MemoryRegionSection *section)
46318f67ceSAlexey Kardashevskiy {
47318f67ceSAlexey Kardashevskiy     VFIOContainer *container = container_of(listener, VFIOContainer,
48318f67ceSAlexey Kardashevskiy                                             prereg_listener);
49318f67ceSAlexey Kardashevskiy     const hwaddr gpa = section->offset_within_address_space;
50318f67ceSAlexey Kardashevskiy     hwaddr end;
51318f67ceSAlexey Kardashevskiy     int ret;
528e3b0cbbSMarc-André Lureau     hwaddr page_mask = qemu_real_host_page_mask();
53318f67ceSAlexey Kardashevskiy     struct vfio_iommu_spapr_register_memory reg = {
54318f67ceSAlexey Kardashevskiy         .argsz = sizeof(reg),
55318f67ceSAlexey Kardashevskiy         .flags = 0,
56318f67ceSAlexey Kardashevskiy     };
57318f67ceSAlexey Kardashevskiy 
58318f67ceSAlexey Kardashevskiy     if (vfio_prereg_listener_skipped_section(section)) {
59318f67ceSAlexey Kardashevskiy         trace_vfio_prereg_listener_region_add_skip(
60318f67ceSAlexey Kardashevskiy                 section->offset_within_address_space,
61318f67ceSAlexey Kardashevskiy                 section->offset_within_address_space +
62318f67ceSAlexey Kardashevskiy                 int128_get64(int128_sub(section->size, int128_one())));
63318f67ceSAlexey Kardashevskiy         return;
64318f67ceSAlexey Kardashevskiy     }
65318f67ceSAlexey Kardashevskiy 
66318f67ceSAlexey Kardashevskiy     if (unlikely((section->offset_within_address_space & ~page_mask) ||
67318f67ceSAlexey Kardashevskiy                  (section->offset_within_region & ~page_mask) ||
68318f67ceSAlexey Kardashevskiy                  (int128_get64(section->size) & ~page_mask))) {
69318f67ceSAlexey Kardashevskiy         error_report("%s received unaligned region", __func__);
70318f67ceSAlexey Kardashevskiy         return;
71318f67ceSAlexey Kardashevskiy     }
72318f67ceSAlexey Kardashevskiy 
73318f67ceSAlexey Kardashevskiy     end = section->offset_within_address_space + int128_get64(section->size);
74318f67ceSAlexey Kardashevskiy     if (gpa >= end) {
75318f67ceSAlexey Kardashevskiy         return;
76318f67ceSAlexey Kardashevskiy     }
77318f67ceSAlexey Kardashevskiy 
78318f67ceSAlexey Kardashevskiy     memory_region_ref(section->mr);
79318f67ceSAlexey Kardashevskiy 
80318f67ceSAlexey Kardashevskiy     reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa);
81318f67ceSAlexey Kardashevskiy     reg.size = end - gpa;
82318f67ceSAlexey Kardashevskiy 
83318f67ceSAlexey Kardashevskiy     ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
84318f67ceSAlexey Kardashevskiy     trace_vfio_prereg_register(reg.vaddr, reg.size, ret ? -errno : 0);
85318f67ceSAlexey Kardashevskiy     if (ret) {
86318f67ceSAlexey Kardashevskiy         /*
87318f67ceSAlexey Kardashevskiy          * On the initfn path, store the first error in the container so we
88318f67ceSAlexey Kardashevskiy          * can gracefully fail.  Runtime, there's not much we can do other
89318f67ceSAlexey Kardashevskiy          * than throw a hardware error.
90318f67ceSAlexey Kardashevskiy          */
91318f67ceSAlexey Kardashevskiy         if (!container->initialized) {
92318f67ceSAlexey Kardashevskiy             if (!container->error) {
93d7d87836SEric Auger                 error_setg_errno(&container->error, -ret,
94d7d87836SEric Auger                                  "Memory registering failed");
95318f67ceSAlexey Kardashevskiy             }
96318f67ceSAlexey Kardashevskiy         } else {
97318f67ceSAlexey Kardashevskiy             hw_error("vfio: Memory registering failed, unable to continue");
98318f67ceSAlexey Kardashevskiy         }
99318f67ceSAlexey Kardashevskiy     }
100318f67ceSAlexey Kardashevskiy }
101318f67ceSAlexey Kardashevskiy 
102318f67ceSAlexey Kardashevskiy static void vfio_prereg_listener_region_del(MemoryListener *listener,
103318f67ceSAlexey Kardashevskiy                                             MemoryRegionSection *section)
104318f67ceSAlexey Kardashevskiy {
105318f67ceSAlexey Kardashevskiy     VFIOContainer *container = container_of(listener, VFIOContainer,
106318f67ceSAlexey Kardashevskiy                                             prereg_listener);
107318f67ceSAlexey Kardashevskiy     const hwaddr gpa = section->offset_within_address_space;
108318f67ceSAlexey Kardashevskiy     hwaddr end;
109318f67ceSAlexey Kardashevskiy     int ret;
1108e3b0cbbSMarc-André Lureau     hwaddr page_mask = qemu_real_host_page_mask();
111318f67ceSAlexey Kardashevskiy     struct vfio_iommu_spapr_register_memory reg = {
112318f67ceSAlexey Kardashevskiy         .argsz = sizeof(reg),
113318f67ceSAlexey Kardashevskiy         .flags = 0,
114318f67ceSAlexey Kardashevskiy     };
115318f67ceSAlexey Kardashevskiy 
116318f67ceSAlexey Kardashevskiy     if (vfio_prereg_listener_skipped_section(section)) {
117318f67ceSAlexey Kardashevskiy         trace_vfio_prereg_listener_region_del_skip(
118318f67ceSAlexey Kardashevskiy                 section->offset_within_address_space,
119318f67ceSAlexey Kardashevskiy                 section->offset_within_address_space +
120318f67ceSAlexey Kardashevskiy                 int128_get64(int128_sub(section->size, int128_one())));
121318f67ceSAlexey Kardashevskiy         return;
122318f67ceSAlexey Kardashevskiy     }
123318f67ceSAlexey Kardashevskiy 
124318f67ceSAlexey Kardashevskiy     if (unlikely((section->offset_within_address_space & ~page_mask) ||
125318f67ceSAlexey Kardashevskiy                  (section->offset_within_region & ~page_mask) ||
126318f67ceSAlexey Kardashevskiy                  (int128_get64(section->size) & ~page_mask))) {
127318f67ceSAlexey Kardashevskiy         error_report("%s received unaligned region", __func__);
128318f67ceSAlexey Kardashevskiy         return;
129318f67ceSAlexey Kardashevskiy     }
130318f67ceSAlexey Kardashevskiy 
131318f67ceSAlexey Kardashevskiy     end = section->offset_within_address_space + int128_get64(section->size);
132318f67ceSAlexey Kardashevskiy     if (gpa >= end) {
133318f67ceSAlexey Kardashevskiy         return;
134318f67ceSAlexey Kardashevskiy     }
135318f67ceSAlexey Kardashevskiy 
136318f67ceSAlexey Kardashevskiy     reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa);
137318f67ceSAlexey Kardashevskiy     reg.size = end - gpa;
138318f67ceSAlexey Kardashevskiy 
139318f67ceSAlexey Kardashevskiy     ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
140318f67ceSAlexey Kardashevskiy     trace_vfio_prereg_unregister(reg.vaddr, reg.size, ret ? -errno : 0);
141318f67ceSAlexey Kardashevskiy }
142318f67ceSAlexey Kardashevskiy 
143770c3b6eSZhenzhong Duan static const MemoryListener vfio_prereg_listener = {
144142518bdSPeter Xu     .name = "vfio-pre-reg",
145318f67ceSAlexey Kardashevskiy     .region_add = vfio_prereg_listener_region_add,
146318f67ceSAlexey Kardashevskiy     .region_del = vfio_prereg_listener_region_del,
147318f67ceSAlexey Kardashevskiy };
1482e4109deSAlexey Kardashevskiy 
149*a17879f0SZhenzhong Duan static int vfio_spapr_remove_window(VFIOContainer *container,
150*a17879f0SZhenzhong Duan                                     hwaddr offset_within_address_space)
151*a17879f0SZhenzhong Duan {
152*a17879f0SZhenzhong Duan     struct vfio_iommu_spapr_tce_remove remove = {
153*a17879f0SZhenzhong Duan         .argsz = sizeof(remove),
154*a17879f0SZhenzhong Duan         .start_addr = offset_within_address_space,
155*a17879f0SZhenzhong Duan     };
156*a17879f0SZhenzhong Duan     int ret;
157*a17879f0SZhenzhong Duan 
158*a17879f0SZhenzhong Duan     ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
159*a17879f0SZhenzhong Duan     if (ret) {
160*a17879f0SZhenzhong Duan         error_report("Failed to remove window at %"PRIx64,
161*a17879f0SZhenzhong Duan                      (uint64_t)remove.start_addr);
162*a17879f0SZhenzhong Duan         return -errno;
163*a17879f0SZhenzhong Duan     }
164*a17879f0SZhenzhong Duan 
165*a17879f0SZhenzhong Duan     trace_vfio_spapr_remove_window(offset_within_address_space);
166*a17879f0SZhenzhong Duan 
167*a17879f0SZhenzhong Duan     return 0;
168*a17879f0SZhenzhong Duan }
169*a17879f0SZhenzhong Duan 
170*a17879f0SZhenzhong Duan static int vfio_spapr_create_window(VFIOContainer *container,
1712e4109deSAlexey Kardashevskiy                                     MemoryRegionSection *section,
1722e4109deSAlexey Kardashevskiy                                     hwaddr *pgsize)
1732e4109deSAlexey Kardashevskiy {
17416107998SAlexey Kardashevskiy     int ret = 0;
1753df9d748SAlexey Kardashevskiy     IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
17679178eddSAlexey Kardashevskiy     uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
17716107998SAlexey Kardashevskiy     unsigned entries, bits_total, bits_per_level, max_levels;
1782e4109deSAlexey Kardashevskiy     struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
179905b7ee4SDavid Hildenbrand     long rampagesize = qemu_minrampagesize();
180c26bc185SAlexey Kardashevskiy 
181c26bc185SAlexey Kardashevskiy     /*
182c26bc185SAlexey Kardashevskiy      * The host might not support the guest supported IOMMU page size,
183c26bc185SAlexey Kardashevskiy      * so we will use smaller physical IOMMU pages to back them.
184c26bc185SAlexey Kardashevskiy      */
1853cdd801bSAlexey Kardashevskiy     if (pagesize > rampagesize) {
1863cdd801bSAlexey Kardashevskiy         pagesize = rampagesize;
187c26bc185SAlexey Kardashevskiy     }
18879178eddSAlexey Kardashevskiy     pgmask = container->pgsizes & (pagesize | (pagesize - 1));
18979178eddSAlexey Kardashevskiy     pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
190c26bc185SAlexey Kardashevskiy     if (!pagesize) {
191c26bc185SAlexey Kardashevskiy         error_report("Host doesn't support page size 0x%"PRIx64
192c26bc185SAlexey Kardashevskiy                      ", the supported mask is 0x%lx",
193c26bc185SAlexey Kardashevskiy                      memory_region_iommu_get_min_page_size(iommu_mr),
194c26bc185SAlexey Kardashevskiy                      container->pgsizes);
195c26bc185SAlexey Kardashevskiy         return -EINVAL;
196c26bc185SAlexey Kardashevskiy     }
1972e4109deSAlexey Kardashevskiy 
1982e4109deSAlexey Kardashevskiy     /*
1992e4109deSAlexey Kardashevskiy      * FIXME: For VFIO iommu types which have KVM acceleration to
2002e4109deSAlexey Kardashevskiy      * avoid bouncing all map/unmaps through qemu this way, this
2012e4109deSAlexey Kardashevskiy      * would be the right place to wire that up (tell the KVM
2022e4109deSAlexey Kardashevskiy      * device emulation the VFIO iommu handles to use).
2032e4109deSAlexey Kardashevskiy      */
2042e4109deSAlexey Kardashevskiy     create.window_size = int128_get64(section->size);
2052e4109deSAlexey Kardashevskiy     create.page_shift = ctz64(pagesize);
2062e4109deSAlexey Kardashevskiy     /*
20716107998SAlexey Kardashevskiy      * SPAPR host supports multilevel TCE tables. We try to guess optimal
20816107998SAlexey Kardashevskiy      * levels number and if this fails (for example due to the host memory
20916107998SAlexey Kardashevskiy      * fragmentation), we increase levels. The DMA address structure is:
21016107998SAlexey Kardashevskiy      * rrrrrrrr rxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xxxxxxxx xxxxxxxx iiiiiiii
21116107998SAlexey Kardashevskiy      * where:
21216107998SAlexey Kardashevskiy      *   r = reserved (bits >= 55 are reserved in the existing hardware)
21316107998SAlexey Kardashevskiy      *   i = IOMMU page offset (64K in this example)
21416107998SAlexey Kardashevskiy      *   x = bits to index a TCE which can be split to equal chunks to index
21516107998SAlexey Kardashevskiy      *      within the level.
21616107998SAlexey Kardashevskiy      * The aim is to split "x" to smaller possible number of levels.
2172e4109deSAlexey Kardashevskiy      */
2182e4109deSAlexey Kardashevskiy     entries = create.window_size >> create.page_shift;
21916107998SAlexey Kardashevskiy     /* bits_total is number of "x" needed */
22016107998SAlexey Kardashevskiy     bits_total = ctz64(entries * sizeof(uint64_t));
22116107998SAlexey Kardashevskiy     /*
22216107998SAlexey Kardashevskiy      * bits_per_level is a safe guess of how much we can allocate per level:
22316107998SAlexey Kardashevskiy      * 8 is the current minimum for CONFIG_FORCE_MAX_ZONEORDER and MAX_ORDER
22416107998SAlexey Kardashevskiy      * is usually bigger than that.
225038adc2fSWei Yang      * Below we look at qemu_real_host_page_size as TCEs are allocated from
226038adc2fSWei Yang      * system pages.
22716107998SAlexey Kardashevskiy      */
2288e3b0cbbSMarc-André Lureau     bits_per_level = ctz64(qemu_real_host_page_size()) + 8;
22916107998SAlexey Kardashevskiy     create.levels = bits_total / bits_per_level;
23016107998SAlexey Kardashevskiy     if (bits_total % bits_per_level) {
23116107998SAlexey Kardashevskiy         ++create.levels;
23216107998SAlexey Kardashevskiy     }
2338e3b0cbbSMarc-André Lureau     max_levels = (64 - create.page_shift) / ctz64(qemu_real_host_page_size());
23416107998SAlexey Kardashevskiy     for ( ; create.levels <= max_levels; ++create.levels) {
2352e4109deSAlexey Kardashevskiy         ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
23616107998SAlexey Kardashevskiy         if (!ret) {
23716107998SAlexey Kardashevskiy             break;
23816107998SAlexey Kardashevskiy         }
23916107998SAlexey Kardashevskiy     }
2402e4109deSAlexey Kardashevskiy     if (ret) {
2412e4109deSAlexey Kardashevskiy         error_report("Failed to create a window, ret = %d (%m)", ret);
2422e4109deSAlexey Kardashevskiy         return -errno;
2432e4109deSAlexey Kardashevskiy     }
2442e4109deSAlexey Kardashevskiy 
2452e4109deSAlexey Kardashevskiy     if (create.start_addr != section->offset_within_address_space) {
2462e4109deSAlexey Kardashevskiy         vfio_spapr_remove_window(container, create.start_addr);
2472e4109deSAlexey Kardashevskiy 
2482e4109deSAlexey Kardashevskiy         error_report("Host doesn't support DMA window at %"HWADDR_PRIx", must be %"PRIx64,
2492e4109deSAlexey Kardashevskiy                      section->offset_within_address_space,
2502e4109deSAlexey Kardashevskiy                      (uint64_t)create.start_addr);
2512e4109deSAlexey Kardashevskiy         return -EINVAL;
2522e4109deSAlexey Kardashevskiy     }
2532e4109deSAlexey Kardashevskiy     trace_vfio_spapr_create_window(create.page_shift,
25416107998SAlexey Kardashevskiy                                    create.levels,
2552e4109deSAlexey Kardashevskiy                                    create.window_size,
2562e4109deSAlexey Kardashevskiy                                    create.start_addr);
2572e4109deSAlexey Kardashevskiy     *pgsize = pagesize;
2582e4109deSAlexey Kardashevskiy 
2592e4109deSAlexey Kardashevskiy     return 0;
2602e4109deSAlexey Kardashevskiy }
2612e4109deSAlexey Kardashevskiy 
262521c8f4eSZhenzhong Duan int vfio_container_add_section_window(VFIOContainer *container,
263521c8f4eSZhenzhong Duan                                       MemoryRegionSection *section,
264521c8f4eSZhenzhong Duan                                       Error **errp)
265521c8f4eSZhenzhong Duan {
266521c8f4eSZhenzhong Duan     VFIOHostDMAWindow *hostwin;
267521c8f4eSZhenzhong Duan     hwaddr pgsize = 0;
268521c8f4eSZhenzhong Duan     int ret;
269521c8f4eSZhenzhong Duan 
270521c8f4eSZhenzhong Duan     if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) {
271521c8f4eSZhenzhong Duan         return 0;
272521c8f4eSZhenzhong Duan     }
273521c8f4eSZhenzhong Duan 
274521c8f4eSZhenzhong Duan     /* For now intersections are not allowed, we may relax this later */
275521c8f4eSZhenzhong Duan     QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
276521c8f4eSZhenzhong Duan         if (ranges_overlap(hostwin->min_iova,
277521c8f4eSZhenzhong Duan                            hostwin->max_iova - hostwin->min_iova + 1,
278521c8f4eSZhenzhong Duan                            section->offset_within_address_space,
279521c8f4eSZhenzhong Duan                            int128_get64(section->size))) {
280521c8f4eSZhenzhong Duan             error_setg(errp,
281521c8f4eSZhenzhong Duan                 "region [0x%"PRIx64",0x%"PRIx64"] overlaps with existing"
282521c8f4eSZhenzhong Duan                 "host DMA window [0x%"PRIx64",0x%"PRIx64"]",
283521c8f4eSZhenzhong Duan                 section->offset_within_address_space,
284521c8f4eSZhenzhong Duan                 section->offset_within_address_space +
285521c8f4eSZhenzhong Duan                     int128_get64(section->size) - 1,
286521c8f4eSZhenzhong Duan                 hostwin->min_iova, hostwin->max_iova);
287521c8f4eSZhenzhong Duan             return -EINVAL;
288521c8f4eSZhenzhong Duan         }
289521c8f4eSZhenzhong Duan     }
290521c8f4eSZhenzhong Duan 
291521c8f4eSZhenzhong Duan     ret = vfio_spapr_create_window(container, section, &pgsize);
292521c8f4eSZhenzhong Duan     if (ret) {
293521c8f4eSZhenzhong Duan         error_setg_errno(errp, -ret, "Failed to create SPAPR window");
294521c8f4eSZhenzhong Duan         return ret;
295521c8f4eSZhenzhong Duan     }
296521c8f4eSZhenzhong Duan 
297521c8f4eSZhenzhong Duan     vfio_host_win_add(container, section->offset_within_address_space,
298521c8f4eSZhenzhong Duan                       section->offset_within_address_space +
299521c8f4eSZhenzhong Duan                       int128_get64(section->size) - 1, pgsize);
300521c8f4eSZhenzhong Duan #ifdef CONFIG_KVM
301521c8f4eSZhenzhong Duan     if (kvm_enabled()) {
302521c8f4eSZhenzhong Duan         VFIOGroup *group;
303521c8f4eSZhenzhong Duan         IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
304521c8f4eSZhenzhong Duan         struct kvm_vfio_spapr_tce param;
305521c8f4eSZhenzhong Duan         struct kvm_device_attr attr = {
306521c8f4eSZhenzhong Duan             .group = KVM_DEV_VFIO_GROUP,
307521c8f4eSZhenzhong Duan             .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
308521c8f4eSZhenzhong Duan             .addr = (uint64_t)(unsigned long)&param,
309521c8f4eSZhenzhong Duan         };
310521c8f4eSZhenzhong Duan 
311521c8f4eSZhenzhong Duan         if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD,
312521c8f4eSZhenzhong Duan                                           &param.tablefd)) {
313521c8f4eSZhenzhong Duan             QLIST_FOREACH(group, &container->group_list, container_next) {
314521c8f4eSZhenzhong Duan                 param.groupfd = group->fd;
315521c8f4eSZhenzhong Duan                 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
316521c8f4eSZhenzhong Duan                     error_setg_errno(errp, errno,
317521c8f4eSZhenzhong Duan                                      "vfio: failed GROUP_SET_SPAPR_TCE for "
318521c8f4eSZhenzhong Duan                                      "KVM VFIO device %d and group fd %d",
319521c8f4eSZhenzhong Duan                                      param.tablefd, param.groupfd);
320521c8f4eSZhenzhong Duan                     return -errno;
321521c8f4eSZhenzhong Duan                 }
322521c8f4eSZhenzhong Duan                 trace_vfio_spapr_group_attach(param.groupfd, param.tablefd);
323521c8f4eSZhenzhong Duan             }
324521c8f4eSZhenzhong Duan         }
325521c8f4eSZhenzhong Duan     }
326521c8f4eSZhenzhong Duan #endif
327521c8f4eSZhenzhong Duan     return 0;
328521c8f4eSZhenzhong Duan }
329521c8f4eSZhenzhong Duan 
330521c8f4eSZhenzhong Duan void vfio_container_del_section_window(VFIOContainer *container,
331521c8f4eSZhenzhong Duan                                        MemoryRegionSection *section)
332521c8f4eSZhenzhong Duan {
333521c8f4eSZhenzhong Duan     if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) {
334521c8f4eSZhenzhong Duan         return;
335521c8f4eSZhenzhong Duan     }
336521c8f4eSZhenzhong Duan 
337521c8f4eSZhenzhong Duan     vfio_spapr_remove_window(container,
338521c8f4eSZhenzhong Duan                              section->offset_within_address_space);
339521c8f4eSZhenzhong Duan     if (vfio_host_win_del(container,
340521c8f4eSZhenzhong Duan                           section->offset_within_address_space,
341521c8f4eSZhenzhong Duan                           section->offset_within_address_space +
342521c8f4eSZhenzhong Duan                           int128_get64(section->size) - 1) < 0) {
343521c8f4eSZhenzhong Duan         hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx,
344521c8f4eSZhenzhong Duan                  __func__, section->offset_within_address_space);
345521c8f4eSZhenzhong Duan     }
346521c8f4eSZhenzhong Duan }
347770c3b6eSZhenzhong Duan 
348770c3b6eSZhenzhong Duan int vfio_spapr_container_init(VFIOContainer *container, Error **errp)
349770c3b6eSZhenzhong Duan {
350770c3b6eSZhenzhong Duan     struct vfio_iommu_spapr_tce_info info;
351770c3b6eSZhenzhong Duan     bool v2 = container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU;
352770c3b6eSZhenzhong Duan     int ret, fd = container->fd;
353770c3b6eSZhenzhong Duan 
354770c3b6eSZhenzhong Duan     /*
355770c3b6eSZhenzhong Duan      * The host kernel code implementing VFIO_IOMMU_DISABLE is called
356770c3b6eSZhenzhong Duan      * when container fd is closed so we do not call it explicitly
357770c3b6eSZhenzhong Duan      * in this file.
358770c3b6eSZhenzhong Duan      */
359770c3b6eSZhenzhong Duan     if (!v2) {
360770c3b6eSZhenzhong Duan         ret = ioctl(fd, VFIO_IOMMU_ENABLE);
361770c3b6eSZhenzhong Duan         if (ret) {
362770c3b6eSZhenzhong Duan             error_setg_errno(errp, errno, "failed to enable container");
363770c3b6eSZhenzhong Duan             return -errno;
364770c3b6eSZhenzhong Duan         }
365770c3b6eSZhenzhong Duan     } else {
366770c3b6eSZhenzhong Duan         container->prereg_listener = vfio_prereg_listener;
367770c3b6eSZhenzhong Duan 
368770c3b6eSZhenzhong Duan         memory_listener_register(&container->prereg_listener,
369770c3b6eSZhenzhong Duan                                  &address_space_memory);
370770c3b6eSZhenzhong Duan         if (container->error) {
371770c3b6eSZhenzhong Duan             ret = -1;
372770c3b6eSZhenzhong Duan             error_propagate_prepend(errp, container->error,
373770c3b6eSZhenzhong Duan                     "RAM memory listener initialization failed: ");
374770c3b6eSZhenzhong Duan             goto listener_unregister_exit;
375770c3b6eSZhenzhong Duan         }
376770c3b6eSZhenzhong Duan     }
377770c3b6eSZhenzhong Duan 
378770c3b6eSZhenzhong Duan     info.argsz = sizeof(info);
379770c3b6eSZhenzhong Duan     ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
380770c3b6eSZhenzhong Duan     if (ret) {
381770c3b6eSZhenzhong Duan         error_setg_errno(errp, errno,
382770c3b6eSZhenzhong Duan                          "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed");
383770c3b6eSZhenzhong Duan         ret = -errno;
384770c3b6eSZhenzhong Duan         goto listener_unregister_exit;
385770c3b6eSZhenzhong Duan     }
386770c3b6eSZhenzhong Duan 
387770c3b6eSZhenzhong Duan     if (v2) {
388770c3b6eSZhenzhong Duan         container->pgsizes = info.ddw.pgsizes;
389770c3b6eSZhenzhong Duan         /*
390770c3b6eSZhenzhong Duan          * There is a default window in just created container.
391770c3b6eSZhenzhong Duan          * To make region_add/del simpler, we better remove this
392770c3b6eSZhenzhong Duan          * window now and let those iommu_listener callbacks
393770c3b6eSZhenzhong Duan          * create/remove them when needed.
394770c3b6eSZhenzhong Duan          */
395770c3b6eSZhenzhong Duan         ret = vfio_spapr_remove_window(container, info.dma32_window_start);
396770c3b6eSZhenzhong Duan         if (ret) {
397770c3b6eSZhenzhong Duan             error_setg_errno(errp, -ret,
398770c3b6eSZhenzhong Duan                              "failed to remove existing window");
399770c3b6eSZhenzhong Duan             goto listener_unregister_exit;
400770c3b6eSZhenzhong Duan         }
401770c3b6eSZhenzhong Duan     } else {
402770c3b6eSZhenzhong Duan         /* The default table uses 4K pages */
403770c3b6eSZhenzhong Duan         container->pgsizes = 0x1000;
404770c3b6eSZhenzhong Duan         vfio_host_win_add(container, info.dma32_window_start,
405770c3b6eSZhenzhong Duan                           info.dma32_window_start +
406770c3b6eSZhenzhong Duan                           info.dma32_window_size - 1,
407770c3b6eSZhenzhong Duan                           0x1000);
408770c3b6eSZhenzhong Duan     }
409770c3b6eSZhenzhong Duan 
410770c3b6eSZhenzhong Duan     return 0;
411770c3b6eSZhenzhong Duan 
412770c3b6eSZhenzhong Duan listener_unregister_exit:
413770c3b6eSZhenzhong Duan     if (v2) {
414770c3b6eSZhenzhong Duan         memory_listener_unregister(&container->prereg_listener);
415770c3b6eSZhenzhong Duan     }
416770c3b6eSZhenzhong Duan     return ret;
417770c3b6eSZhenzhong Duan }
418770c3b6eSZhenzhong Duan 
419770c3b6eSZhenzhong Duan void vfio_spapr_container_deinit(VFIOContainer *container)
420770c3b6eSZhenzhong Duan {
421770c3b6eSZhenzhong Duan     if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
422770c3b6eSZhenzhong Duan         memory_listener_unregister(&container->prereg_listener);
423770c3b6eSZhenzhong Duan     }
424770c3b6eSZhenzhong Duan }
425