4 * Copyright (c) 2020 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
23 #include "qemu-common.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/virtio/virtio.h"
26 #include "sysemu/kvm.h"
27 #include "qapi/error.h"
28 #include "qemu/error-report.h"
31 #include "standard-headers/linux/virtio_ids.h"
33 #include "hw/virtio/virtio-bus.h"
34 #include "hw/virtio/virtio-access.h"
35 #include "hw/virtio/virtio-iommu.h"
36 #include "hw/pci/pci_bus.h"
37 #include "hw/pci/pci.h"
40 #define VIOMMU_DEFAULT_QUEUE_SIZE 256
41 #define VIOMMU_PROBE_SIZE 512
43 typedef struct VirtIOIOMMUDomain
{
46 QLIST_HEAD(, VirtIOIOMMUEndpoint
) endpoint_list
;
49 typedef struct VirtIOIOMMUEndpoint
{
51 VirtIOIOMMUDomain
*domain
;
52 IOMMUMemoryRegion
*iommu_mr
;
53 QLIST_ENTRY(VirtIOIOMMUEndpoint
) next
;
54 } VirtIOIOMMUEndpoint
;
56 typedef struct VirtIOIOMMUInterval
{
59 } VirtIOIOMMUInterval
;
61 typedef struct VirtIOIOMMUMapping
{
66 static inline uint16_t virtio_iommu_get_bdf(IOMMUDevice
*dev
)
68 return PCI_BUILD_BDF(pci_bus_num(dev
->bus
), dev
->devfn
);
72 * The bus number is used for lookup when SID based operations occur.
73 * In that case we lazily populate the IOMMUPciBus array from the bus hash
74 * table. At the time the IOMMUPciBus is created (iommu_find_add_as), the bus
75 * numbers may not be always initialized yet.
77 static IOMMUPciBus
*iommu_find_iommu_pcibus(VirtIOIOMMU
*s
, uint8_t bus_num
)
79 IOMMUPciBus
*iommu_pci_bus
= s
->iommu_pcibus_by_bus_num
[bus_num
];
84 g_hash_table_iter_init(&iter
, s
->as_by_busptr
);
85 while (g_hash_table_iter_next(&iter
, NULL
, (void **)&iommu_pci_bus
)) {
86 if (pci_bus_num(iommu_pci_bus
->bus
) == bus_num
) {
87 s
->iommu_pcibus_by_bus_num
[bus_num
] = iommu_pci_bus
;
96 static IOMMUMemoryRegion
*virtio_iommu_mr(VirtIOIOMMU
*s
, uint32_t sid
)
99 IOMMUPciBus
*iommu_pci_bus
;
102 bus_n
= PCI_BUS_NUM(sid
);
103 iommu_pci_bus
= iommu_find_iommu_pcibus(s
, bus_n
);
105 devfn
= sid
& (PCI_DEVFN_MAX
- 1);
106 dev
= iommu_pci_bus
->pbdev
[devfn
];
108 return &dev
->iommu_mr
;
114 static gint
interval_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
116 VirtIOIOMMUInterval
*inta
= (VirtIOIOMMUInterval
*)a
;
117 VirtIOIOMMUInterval
*intb
= (VirtIOIOMMUInterval
*)b
;
119 if (inta
->high
< intb
->low
) {
121 } else if (intb
->high
< inta
->low
) {
128 static void virtio_iommu_notify_map(IOMMUMemoryRegion
*mr
, hwaddr virt_start
,
129 hwaddr virt_end
, hwaddr paddr
,
133 IOMMUAccessFlags perm
= IOMMU_ACCESS_FLAG(flags
& VIRTIO_IOMMU_MAP_F_READ
,
134 flags
& VIRTIO_IOMMU_MAP_F_WRITE
);
136 if (!(mr
->iommu_notify_flags
& IOMMU_NOTIFIER_MAP
) ||
137 (flags
& VIRTIO_IOMMU_MAP_F_MMIO
) || !perm
) {
141 trace_virtio_iommu_notify_map(mr
->parent_obj
.name
, virt_start
, virt_end
,
144 event
.type
= IOMMU_NOTIFIER_MAP
;
145 event
.entry
.target_as
= &address_space_memory
;
146 event
.entry
.addr_mask
= virt_end
- virt_start
;
147 event
.entry
.iova
= virt_start
;
148 event
.entry
.perm
= perm
;
149 event
.entry
.translated_addr
= paddr
;
151 memory_region_notify_iommu(mr
, 0, event
);
154 static void virtio_iommu_notify_unmap(IOMMUMemoryRegion
*mr
, hwaddr virt_start
,
158 uint64_t delta
= virt_end
- virt_start
;
160 if (!(mr
->iommu_notify_flags
& IOMMU_NOTIFIER_UNMAP
)) {
164 trace_virtio_iommu_notify_unmap(mr
->parent_obj
.name
, virt_start
, virt_end
);
166 event
.type
= IOMMU_NOTIFIER_UNMAP
;
167 event
.entry
.target_as
= &address_space_memory
;
168 event
.entry
.perm
= IOMMU_NONE
;
169 event
.entry
.translated_addr
= 0;
170 event
.entry
.addr_mask
= delta
;
171 event
.entry
.iova
= virt_start
;
173 if (delta
== UINT64_MAX
) {
174 memory_region_notify_iommu(mr
, 0, event
);
178 while (virt_start
!= virt_end
+ 1) {
179 uint64_t mask
= dma_aligned_pow2_mask(virt_start
, virt_end
, 64);
181 event
.entry
.addr_mask
= mask
;
182 event
.entry
.iova
= virt_start
;
183 memory_region_notify_iommu(mr
, 0, event
);
184 virt_start
+= mask
+ 1;
188 static gboolean
virtio_iommu_notify_unmap_cb(gpointer key
, gpointer value
,
191 VirtIOIOMMUInterval
*interval
= (VirtIOIOMMUInterval
*) key
;
192 IOMMUMemoryRegion
*mr
= (IOMMUMemoryRegion
*) data
;
194 virtio_iommu_notify_unmap(mr
, interval
->low
, interval
->high
);
199 static gboolean
virtio_iommu_notify_map_cb(gpointer key
, gpointer value
,
202 VirtIOIOMMUMapping
*mapping
= (VirtIOIOMMUMapping
*) value
;
203 VirtIOIOMMUInterval
*interval
= (VirtIOIOMMUInterval
*) key
;
204 IOMMUMemoryRegion
*mr
= (IOMMUMemoryRegion
*) data
;
206 virtio_iommu_notify_map(mr
, interval
->low
, interval
->high
,
207 mapping
->phys_addr
, mapping
->flags
);
212 static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint
*ep
)
214 VirtIOIOMMUDomain
*domain
= ep
->domain
;
219 g_tree_foreach(domain
->mappings
, virtio_iommu_notify_unmap_cb
,
221 QLIST_REMOVE(ep
, next
);
225 static VirtIOIOMMUEndpoint
*virtio_iommu_get_endpoint(VirtIOIOMMU
*s
,
228 VirtIOIOMMUEndpoint
*ep
;
229 IOMMUMemoryRegion
*mr
;
231 ep
= g_tree_lookup(s
->endpoints
, GUINT_TO_POINTER(ep_id
));
235 mr
= virtio_iommu_mr(s
, ep_id
);
239 ep
= g_malloc0(sizeof(*ep
));
242 trace_virtio_iommu_get_endpoint(ep_id
);
243 g_tree_insert(s
->endpoints
, GUINT_TO_POINTER(ep_id
), ep
);
247 static void virtio_iommu_put_endpoint(gpointer data
)
249 VirtIOIOMMUEndpoint
*ep
= (VirtIOIOMMUEndpoint
*)data
;
252 virtio_iommu_detach_endpoint_from_domain(ep
);
255 trace_virtio_iommu_put_endpoint(ep
->id
);
259 static VirtIOIOMMUDomain
*virtio_iommu_get_domain(VirtIOIOMMU
*s
,
262 VirtIOIOMMUDomain
*domain
;
264 domain
= g_tree_lookup(s
->domains
, GUINT_TO_POINTER(domain_id
));
268 domain
= g_malloc0(sizeof(*domain
));
269 domain
->id
= domain_id
;
270 domain
->mappings
= g_tree_new_full((GCompareDataFunc
)interval_cmp
,
271 NULL
, (GDestroyNotify
)g_free
,
272 (GDestroyNotify
)g_free
);
273 g_tree_insert(s
->domains
, GUINT_TO_POINTER(domain_id
), domain
);
274 QLIST_INIT(&domain
->endpoint_list
);
275 trace_virtio_iommu_get_domain(domain_id
);
279 static void virtio_iommu_put_domain(gpointer data
)
281 VirtIOIOMMUDomain
*domain
= (VirtIOIOMMUDomain
*)data
;
282 VirtIOIOMMUEndpoint
*iter
, *tmp
;
284 QLIST_FOREACH_SAFE(iter
, &domain
->endpoint_list
, next
, tmp
) {
285 virtio_iommu_detach_endpoint_from_domain(iter
);
287 g_tree_destroy(domain
->mappings
);
288 trace_virtio_iommu_put_domain(domain
->id
);
292 static AddressSpace
*virtio_iommu_find_add_as(PCIBus
*bus
, void *opaque
,
295 VirtIOIOMMU
*s
= opaque
;
296 IOMMUPciBus
*sbus
= g_hash_table_lookup(s
->as_by_busptr
, bus
);
297 static uint32_t mr_index
;
301 sbus
= g_malloc0(sizeof(IOMMUPciBus
) +
302 sizeof(IOMMUDevice
*) * PCI_DEVFN_MAX
);
304 g_hash_table_insert(s
->as_by_busptr
, bus
, sbus
);
307 sdev
= sbus
->pbdev
[devfn
];
309 char *name
= g_strdup_printf("%s-%d-%d",
310 TYPE_VIRTIO_IOMMU_MEMORY_REGION
,
312 sdev
= sbus
->pbdev
[devfn
] = g_malloc0(sizeof(IOMMUDevice
));
318 trace_virtio_iommu_init_iommu_mr(name
);
320 memory_region_init_iommu(&sdev
->iommu_mr
, sizeof(sdev
->iommu_mr
),
321 TYPE_VIRTIO_IOMMU_MEMORY_REGION
,
324 address_space_init(&sdev
->as
,
325 MEMORY_REGION(&sdev
->iommu_mr
), TYPE_VIRTIO_IOMMU
);
331 static int virtio_iommu_attach(VirtIOIOMMU
*s
,
332 struct virtio_iommu_req_attach
*req
)
334 uint32_t domain_id
= le32_to_cpu(req
->domain
);
335 uint32_t ep_id
= le32_to_cpu(req
->endpoint
);
336 VirtIOIOMMUDomain
*domain
;
337 VirtIOIOMMUEndpoint
*ep
;
339 trace_virtio_iommu_attach(domain_id
, ep_id
);
341 ep
= virtio_iommu_get_endpoint(s
, ep_id
);
343 return VIRTIO_IOMMU_S_NOENT
;
347 VirtIOIOMMUDomain
*previous_domain
= ep
->domain
;
349 * the device is already attached to a domain,
352 virtio_iommu_detach_endpoint_from_domain(ep
);
353 if (QLIST_EMPTY(&previous_domain
->endpoint_list
)) {
354 g_tree_remove(s
->domains
, GUINT_TO_POINTER(previous_domain
->id
));
358 domain
= virtio_iommu_get_domain(s
, domain_id
);
359 QLIST_INSERT_HEAD(&domain
->endpoint_list
, ep
, next
);
363 /* Replay domain mappings on the associated memory region */
364 g_tree_foreach(domain
->mappings
, virtio_iommu_notify_map_cb
,
367 return VIRTIO_IOMMU_S_OK
;
370 static int virtio_iommu_detach(VirtIOIOMMU
*s
,
371 struct virtio_iommu_req_detach
*req
)
373 uint32_t domain_id
= le32_to_cpu(req
->domain
);
374 uint32_t ep_id
= le32_to_cpu(req
->endpoint
);
375 VirtIOIOMMUDomain
*domain
;
376 VirtIOIOMMUEndpoint
*ep
;
378 trace_virtio_iommu_detach(domain_id
, ep_id
);
380 ep
= g_tree_lookup(s
->endpoints
, GUINT_TO_POINTER(ep_id
));
382 return VIRTIO_IOMMU_S_NOENT
;
387 if (!domain
|| domain
->id
!= domain_id
) {
388 return VIRTIO_IOMMU_S_INVAL
;
391 virtio_iommu_detach_endpoint_from_domain(ep
);
393 if (QLIST_EMPTY(&domain
->endpoint_list
)) {
394 g_tree_remove(s
->domains
, GUINT_TO_POINTER(domain
->id
));
396 return VIRTIO_IOMMU_S_OK
;
399 static int virtio_iommu_map(VirtIOIOMMU
*s
,
400 struct virtio_iommu_req_map
*req
)
402 uint32_t domain_id
= le32_to_cpu(req
->domain
);
403 uint64_t phys_start
= le64_to_cpu(req
->phys_start
);
404 uint64_t virt_start
= le64_to_cpu(req
->virt_start
);
405 uint64_t virt_end
= le64_to_cpu(req
->virt_end
);
406 uint32_t flags
= le32_to_cpu(req
->flags
);
407 VirtIOIOMMUDomain
*domain
;
408 VirtIOIOMMUInterval
*interval
;
409 VirtIOIOMMUMapping
*mapping
;
410 VirtIOIOMMUEndpoint
*ep
;
412 if (flags
& ~VIRTIO_IOMMU_MAP_F_MASK
) {
413 return VIRTIO_IOMMU_S_INVAL
;
416 domain
= g_tree_lookup(s
->domains
, GUINT_TO_POINTER(domain_id
));
418 return VIRTIO_IOMMU_S_NOENT
;
421 interval
= g_malloc0(sizeof(*interval
));
423 interval
->low
= virt_start
;
424 interval
->high
= virt_end
;
426 mapping
= g_tree_lookup(domain
->mappings
, (gpointer
)interval
);
429 return VIRTIO_IOMMU_S_INVAL
;
432 trace_virtio_iommu_map(domain_id
, virt_start
, virt_end
, phys_start
, flags
);
434 mapping
= g_malloc0(sizeof(*mapping
));
435 mapping
->phys_addr
= phys_start
;
436 mapping
->flags
= flags
;
438 g_tree_insert(domain
->mappings
, interval
, mapping
);
440 QLIST_FOREACH(ep
, &domain
->endpoint_list
, next
) {
441 virtio_iommu_notify_map(ep
->iommu_mr
, virt_start
, virt_end
, phys_start
,
445 return VIRTIO_IOMMU_S_OK
;
448 static int virtio_iommu_unmap(VirtIOIOMMU
*s
,
449 struct virtio_iommu_req_unmap
*req
)
451 uint32_t domain_id
= le32_to_cpu(req
->domain
);
452 uint64_t virt_start
= le64_to_cpu(req
->virt_start
);
453 uint64_t virt_end
= le64_to_cpu(req
->virt_end
);
454 VirtIOIOMMUMapping
*iter_val
;
455 VirtIOIOMMUInterval interval
, *iter_key
;
456 VirtIOIOMMUDomain
*domain
;
457 VirtIOIOMMUEndpoint
*ep
;
458 int ret
= VIRTIO_IOMMU_S_OK
;
460 trace_virtio_iommu_unmap(domain_id
, virt_start
, virt_end
);
462 domain
= g_tree_lookup(s
->domains
, GUINT_TO_POINTER(domain_id
));
464 return VIRTIO_IOMMU_S_NOENT
;
466 interval
.low
= virt_start
;
467 interval
.high
= virt_end
;
469 while (g_tree_lookup_extended(domain
->mappings
, &interval
,
470 (void **)&iter_key
, (void**)&iter_val
)) {
471 uint64_t current_low
= iter_key
->low
;
472 uint64_t current_high
= iter_key
->high
;
474 if (interval
.low
<= current_low
&& interval
.high
>= current_high
) {
475 QLIST_FOREACH(ep
, &domain
->endpoint_list
, next
) {
476 virtio_iommu_notify_unmap(ep
->iommu_mr
, current_low
,
479 g_tree_remove(domain
->mappings
, iter_key
);
480 trace_virtio_iommu_unmap_done(domain_id
, current_low
, current_high
);
482 ret
= VIRTIO_IOMMU_S_RANGE
;
489 static ssize_t
virtio_iommu_fill_resv_mem_prop(VirtIOIOMMU
*s
, uint32_t ep
,
490 uint8_t *buf
, size_t free
)
492 struct virtio_iommu_probe_resv_mem prop
= {};
493 size_t size
= sizeof(prop
), length
= size
- sizeof(prop
.head
), total
;
496 total
= size
* s
->nb_reserved_regions
;
502 for (i
= 0; i
< s
->nb_reserved_regions
; i
++) {
503 unsigned subtype
= s
->reserved_regions
[i
].type
;
505 assert(subtype
== VIRTIO_IOMMU_RESV_MEM_T_RESERVED
||
506 subtype
== VIRTIO_IOMMU_RESV_MEM_T_MSI
);
507 prop
.head
.type
= cpu_to_le16(VIRTIO_IOMMU_PROBE_T_RESV_MEM
);
508 prop
.head
.length
= cpu_to_le16(length
);
509 prop
.subtype
= subtype
;
510 prop
.start
= cpu_to_le64(s
->reserved_regions
[i
].low
);
511 prop
.end
= cpu_to_le64(s
->reserved_regions
[i
].high
);
513 memcpy(buf
, &prop
, size
);
515 trace_virtio_iommu_fill_resv_property(ep
, prop
.subtype
,
516 prop
.start
, prop
.end
);
523 * virtio_iommu_probe - Fill the probe request buffer with
524 * the properties the device is able to return
526 static int virtio_iommu_probe(VirtIOIOMMU
*s
,
527 struct virtio_iommu_req_probe
*req
,
530 uint32_t ep_id
= le32_to_cpu(req
->endpoint
);
531 size_t free
= VIOMMU_PROBE_SIZE
;
534 if (!virtio_iommu_mr(s
, ep_id
)) {
535 return VIRTIO_IOMMU_S_NOENT
;
538 count
= virtio_iommu_fill_resv_mem_prop(s
, ep_id
, buf
, free
);
540 return VIRTIO_IOMMU_S_INVAL
;
545 return VIRTIO_IOMMU_S_OK
;
548 static int virtio_iommu_iov_to_req(struct iovec
*iov
,
549 unsigned int iov_cnt
,
550 void *req
, size_t req_sz
)
552 size_t sz
, payload_sz
= req_sz
- sizeof(struct virtio_iommu_req_tail
);
554 sz
= iov_to_buf(iov
, iov_cnt
, 0, req
, payload_sz
);
555 if (unlikely(sz
!= payload_sz
)) {
556 return VIRTIO_IOMMU_S_INVAL
;
561 #define virtio_iommu_handle_req(__req) \
562 static int virtio_iommu_handle_ ## __req(VirtIOIOMMU *s, \
564 unsigned int iov_cnt) \
566 struct virtio_iommu_req_ ## __req req; \
567 int ret = virtio_iommu_iov_to_req(iov, iov_cnt, &req, sizeof(req)); \
569 return ret ? ret : virtio_iommu_ ## __req(s, &req); \
572 virtio_iommu_handle_req(attach
)
573 virtio_iommu_handle_req(detach
)
574 virtio_iommu_handle_req(map
)
575 virtio_iommu_handle_req(unmap
)
577 static int virtio_iommu_handle_probe(VirtIOIOMMU
*s
,
579 unsigned int iov_cnt
,
582 struct virtio_iommu_req_probe req
;
583 int ret
= virtio_iommu_iov_to_req(iov
, iov_cnt
, &req
, sizeof(req
));
585 return ret
? ret
: virtio_iommu_probe(s
, &req
, buf
);
588 static void virtio_iommu_handle_command(VirtIODevice
*vdev
, VirtQueue
*vq
)
590 VirtIOIOMMU
*s
= VIRTIO_IOMMU(vdev
);
591 struct virtio_iommu_req_head head
;
592 struct virtio_iommu_req_tail tail
= {};
593 size_t output_size
= sizeof(tail
), sz
;
594 VirtQueueElement
*elem
;
595 unsigned int iov_cnt
;
600 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
605 if (iov_size(elem
->in_sg
, elem
->in_num
) < sizeof(tail
) ||
606 iov_size(elem
->out_sg
, elem
->out_num
) < sizeof(head
)) {
607 virtio_error(vdev
, "virtio-iommu bad head/tail size");
608 virtqueue_detach_element(vq
, elem
, 0);
613 iov_cnt
= elem
->out_num
;
615 sz
= iov_to_buf(iov
, iov_cnt
, 0, &head
, sizeof(head
));
616 if (unlikely(sz
!= sizeof(head
))) {
617 tail
.status
= VIRTIO_IOMMU_S_DEVERR
;
620 qemu_mutex_lock(&s
->mutex
);
622 case VIRTIO_IOMMU_T_ATTACH
:
623 tail
.status
= virtio_iommu_handle_attach(s
, iov
, iov_cnt
);
625 case VIRTIO_IOMMU_T_DETACH
:
626 tail
.status
= virtio_iommu_handle_detach(s
, iov
, iov_cnt
);
628 case VIRTIO_IOMMU_T_MAP
:
629 tail
.status
= virtio_iommu_handle_map(s
, iov
, iov_cnt
);
631 case VIRTIO_IOMMU_T_UNMAP
:
632 tail
.status
= virtio_iommu_handle_unmap(s
, iov
, iov_cnt
);
634 case VIRTIO_IOMMU_T_PROBE
:
636 struct virtio_iommu_req_tail
*ptail
;
638 output_size
= s
->config
.probe_size
+ sizeof(tail
);
639 buf
= g_malloc0(output_size
);
641 ptail
= (struct virtio_iommu_req_tail
*)
642 (buf
+ s
->config
.probe_size
);
643 ptail
->status
= virtio_iommu_handle_probe(s
, iov
, iov_cnt
, buf
);
647 tail
.status
= VIRTIO_IOMMU_S_UNSUPP
;
649 qemu_mutex_unlock(&s
->mutex
);
652 sz
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
653 buf
? buf
: &tail
, output_size
);
654 assert(sz
== output_size
);
656 virtqueue_push(vq
, elem
, sz
);
657 virtio_notify(vdev
, vq
);
663 static void virtio_iommu_report_fault(VirtIOIOMMU
*viommu
, uint8_t reason
,
664 int flags
, uint32_t endpoint
,
667 VirtIODevice
*vdev
= &viommu
->parent_obj
;
668 VirtQueue
*vq
= viommu
->event_vq
;
669 struct virtio_iommu_fault fault
;
670 VirtQueueElement
*elem
;
673 memset(&fault
, 0, sizeof(fault
));
674 fault
.reason
= reason
;
675 fault
.flags
= cpu_to_le32(flags
);
676 fault
.endpoint
= cpu_to_le32(endpoint
);
677 fault
.address
= cpu_to_le64(address
);
679 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
683 "no buffer available in event queue to report event");
687 if (iov_size(elem
->in_sg
, elem
->in_num
) < sizeof(fault
)) {
688 virtio_error(vdev
, "error buffer of wrong size");
689 virtqueue_detach_element(vq
, elem
, 0);
694 sz
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
695 &fault
, sizeof(fault
));
696 assert(sz
== sizeof(fault
));
698 trace_virtio_iommu_report_fault(reason
, flags
, endpoint
, address
);
699 virtqueue_push(vq
, elem
, sz
);
700 virtio_notify(vdev
, vq
);
705 static IOMMUTLBEntry
virtio_iommu_translate(IOMMUMemoryRegion
*mr
, hwaddr addr
,
706 IOMMUAccessFlags flag
,
709 IOMMUDevice
*sdev
= container_of(mr
, IOMMUDevice
, iommu_mr
);
710 VirtIOIOMMUInterval interval
, *mapping_key
;
711 VirtIOIOMMUMapping
*mapping_value
;
712 VirtIOIOMMU
*s
= sdev
->viommu
;
713 bool read_fault
, write_fault
;
714 VirtIOIOMMUEndpoint
*ep
;
721 interval
.high
= addr
+ 1;
723 IOMMUTLBEntry entry
= {
724 .target_as
= &address_space_memory
,
726 .translated_addr
= addr
,
727 .addr_mask
= (1 << ctz32(s
->config
.page_size_mask
)) - 1,
731 bypass_allowed
= virtio_vdev_has_feature(&s
->parent_obj
,
732 VIRTIO_IOMMU_F_BYPASS
);
734 sid
= virtio_iommu_get_bdf(sdev
);
736 trace_virtio_iommu_translate(mr
->parent_obj
.name
, sid
, addr
, flag
);
737 qemu_mutex_lock(&s
->mutex
);
739 ep
= g_tree_lookup(s
->endpoints
, GUINT_TO_POINTER(sid
));
741 if (!bypass_allowed
) {
742 error_report_once("%s sid=%d is not known!!", __func__
, sid
);
743 virtio_iommu_report_fault(s
, VIRTIO_IOMMU_FAULT_R_UNKNOWN
,
744 VIRTIO_IOMMU_FAULT_F_ADDRESS
,
752 for (i
= 0; i
< s
->nb_reserved_regions
; i
++) {
753 ReservedRegion
*reg
= &s
->reserved_regions
[i
];
755 if (addr
>= reg
->low
&& addr
<= reg
->high
) {
757 case VIRTIO_IOMMU_RESV_MEM_T_MSI
:
760 case VIRTIO_IOMMU_RESV_MEM_T_RESERVED
:
762 virtio_iommu_report_fault(s
, VIRTIO_IOMMU_FAULT_R_MAPPING
,
763 VIRTIO_IOMMU_FAULT_F_ADDRESS
,
772 if (!bypass_allowed
) {
773 error_report_once("%s %02x:%02x.%01x not attached to any domain",
774 __func__
, PCI_BUS_NUM(sid
),
775 PCI_SLOT(sid
), PCI_FUNC(sid
));
776 virtio_iommu_report_fault(s
, VIRTIO_IOMMU_FAULT_R_DOMAIN
,
777 VIRTIO_IOMMU_FAULT_F_ADDRESS
,
785 found
= g_tree_lookup_extended(ep
->domain
->mappings
, (gpointer
)(&interval
),
786 (void **)&mapping_key
,
787 (void **)&mapping_value
);
789 error_report_once("%s no mapping for 0x%"PRIx64
" for sid=%d",
790 __func__
, addr
, sid
);
791 virtio_iommu_report_fault(s
, VIRTIO_IOMMU_FAULT_R_MAPPING
,
792 VIRTIO_IOMMU_FAULT_F_ADDRESS
,
797 read_fault
= (flag
& IOMMU_RO
) &&
798 !(mapping_value
->flags
& VIRTIO_IOMMU_MAP_F_READ
);
799 write_fault
= (flag
& IOMMU_WO
) &&
800 !(mapping_value
->flags
& VIRTIO_IOMMU_MAP_F_WRITE
);
802 flags
= read_fault
? VIRTIO_IOMMU_FAULT_F_READ
: 0;
803 flags
|= write_fault
? VIRTIO_IOMMU_FAULT_F_WRITE
: 0;
805 error_report_once("%s permission error on 0x%"PRIx64
"(%d): allowed=%d",
806 __func__
, addr
, flag
, mapping_value
->flags
);
807 flags
|= VIRTIO_IOMMU_FAULT_F_ADDRESS
;
808 virtio_iommu_report_fault(s
, VIRTIO_IOMMU_FAULT_R_MAPPING
,
809 flags
| VIRTIO_IOMMU_FAULT_F_ADDRESS
,
813 entry
.translated_addr
= addr
- mapping_key
->low
+ mapping_value
->phys_addr
;
815 trace_virtio_iommu_translate_out(addr
, entry
.translated_addr
, sid
);
818 qemu_mutex_unlock(&s
->mutex
);
822 static void virtio_iommu_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
824 VirtIOIOMMU
*dev
= VIRTIO_IOMMU(vdev
);
825 struct virtio_iommu_config
*config
= &dev
->config
;
827 trace_virtio_iommu_get_config(config
->page_size_mask
,
828 config
->input_range
.start
,
829 config
->input_range
.end
,
830 config
->domain_range
.end
,
832 memcpy(config_data
, &dev
->config
, sizeof(struct virtio_iommu_config
));
835 static void virtio_iommu_set_config(VirtIODevice
*vdev
,
836 const uint8_t *config_data
)
838 struct virtio_iommu_config config
;
840 memcpy(&config
, config_data
, sizeof(struct virtio_iommu_config
));
841 trace_virtio_iommu_set_config(config
.page_size_mask
,
842 config
.input_range
.start
,
843 config
.input_range
.end
,
844 config
.domain_range
.end
,
848 static uint64_t virtio_iommu_get_features(VirtIODevice
*vdev
, uint64_t f
,
851 VirtIOIOMMU
*dev
= VIRTIO_IOMMU(vdev
);
854 trace_virtio_iommu_get_features(f
);
858 static gint
int_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
860 guint ua
= GPOINTER_TO_UINT(a
);
861 guint ub
= GPOINTER_TO_UINT(b
);
862 return (ua
> ub
) - (ua
< ub
);
865 static gboolean
virtio_iommu_remap(gpointer key
, gpointer value
, gpointer data
)
867 VirtIOIOMMUMapping
*mapping
= (VirtIOIOMMUMapping
*) value
;
868 VirtIOIOMMUInterval
*interval
= (VirtIOIOMMUInterval
*) key
;
869 IOMMUMemoryRegion
*mr
= (IOMMUMemoryRegion
*) data
;
871 trace_virtio_iommu_remap(mr
->parent_obj
.name
, interval
->low
, interval
->high
,
873 virtio_iommu_notify_map(mr
, interval
->low
, interval
->high
,
874 mapping
->phys_addr
, mapping
->flags
);
878 static void virtio_iommu_replay(IOMMUMemoryRegion
*mr
, IOMMUNotifier
*n
)
880 IOMMUDevice
*sdev
= container_of(mr
, IOMMUDevice
, iommu_mr
);
881 VirtIOIOMMU
*s
= sdev
->viommu
;
883 VirtIOIOMMUEndpoint
*ep
;
885 sid
= virtio_iommu_get_bdf(sdev
);
887 qemu_mutex_lock(&s
->mutex
);
893 ep
= g_tree_lookup(s
->endpoints
, GUINT_TO_POINTER(sid
));
894 if (!ep
|| !ep
->domain
) {
898 g_tree_foreach(ep
->domain
->mappings
, virtio_iommu_remap
, mr
);
901 qemu_mutex_unlock(&s
->mutex
);
904 static int virtio_iommu_notify_flag_changed(IOMMUMemoryRegion
*iommu_mr
,
905 IOMMUNotifierFlag old
,
906 IOMMUNotifierFlag
new,
909 if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP
) {
910 error_setg(errp
, "Virtio-iommu does not support dev-iotlb yet");
914 if (old
== IOMMU_NOTIFIER_NONE
) {
915 trace_virtio_iommu_notify_flag_add(iommu_mr
->parent_obj
.name
);
916 } else if (new == IOMMU_NOTIFIER_NONE
) {
917 trace_virtio_iommu_notify_flag_del(iommu_mr
->parent_obj
.name
);
923 * The default mask (TARGET_PAGE_MASK) is the smallest supported guest granule,
924 * for example 0xfffffffffffff000. When an assigned device has page size
925 * restrictions due to the hardware IOMMU configuration, apply this restriction
928 static int virtio_iommu_set_page_size_mask(IOMMUMemoryRegion
*mr
,
932 IOMMUDevice
*sdev
= container_of(mr
, IOMMUDevice
, iommu_mr
);
933 VirtIOIOMMU
*s
= sdev
->viommu
;
934 uint64_t cur_mask
= s
->config
.page_size_mask
;
936 trace_virtio_iommu_set_page_size_mask(mr
->parent_obj
.name
, cur_mask
,
939 if ((cur_mask
& new_mask
) == 0) {
940 error_setg(errp
, "virtio-iommu page mask 0x%"PRIx64
941 " is incompatible with mask 0x%"PRIx64
, cur_mask
, new_mask
);
946 * After the machine is finalized, we can't change the mask anymore. If by
947 * chance the hotplugged device supports the same granule, we can still
948 * accept it. Having a different masks is possible but the guest will use
949 * sub-optimal block sizes, so warn about it.
951 if (phase_check(PHASE_MACHINE_READY
)) {
952 int new_granule
= ctz64(new_mask
);
953 int cur_granule
= ctz64(cur_mask
);
955 if (new_granule
!= cur_granule
) {
956 error_setg(errp
, "virtio-iommu page mask 0x%"PRIx64
957 " is incompatible with mask 0x%"PRIx64
, cur_mask
,
960 } else if (new_mask
!= cur_mask
) {
961 warn_report("virtio-iommu page mask 0x%"PRIx64
962 " does not match 0x%"PRIx64
, cur_mask
, new_mask
);
967 s
->config
.page_size_mask
&= new_mask
;
971 static void virtio_iommu_device_realize(DeviceState
*dev
, Error
**errp
)
973 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
974 VirtIOIOMMU
*s
= VIRTIO_IOMMU(dev
);
976 virtio_init(vdev
, "virtio-iommu", VIRTIO_ID_IOMMU
,
977 sizeof(struct virtio_iommu_config
));
979 memset(s
->iommu_pcibus_by_bus_num
, 0, sizeof(s
->iommu_pcibus_by_bus_num
));
981 s
->req_vq
= virtio_add_queue(vdev
, VIOMMU_DEFAULT_QUEUE_SIZE
,
982 virtio_iommu_handle_command
);
983 s
->event_vq
= virtio_add_queue(vdev
, VIOMMU_DEFAULT_QUEUE_SIZE
, NULL
);
985 s
->config
.page_size_mask
= TARGET_PAGE_MASK
;
986 s
->config
.input_range
.end
= -1UL;
987 s
->config
.domain_range
.end
= 32;
988 s
->config
.probe_size
= VIOMMU_PROBE_SIZE
;
990 virtio_add_feature(&s
->features
, VIRTIO_RING_F_EVENT_IDX
);
991 virtio_add_feature(&s
->features
, VIRTIO_RING_F_INDIRECT_DESC
);
992 virtio_add_feature(&s
->features
, VIRTIO_F_VERSION_1
);
993 virtio_add_feature(&s
->features
, VIRTIO_IOMMU_F_INPUT_RANGE
);
994 virtio_add_feature(&s
->features
, VIRTIO_IOMMU_F_DOMAIN_RANGE
);
995 virtio_add_feature(&s
->features
, VIRTIO_IOMMU_F_MAP_UNMAP
);
996 virtio_add_feature(&s
->features
, VIRTIO_IOMMU_F_BYPASS
);
997 virtio_add_feature(&s
->features
, VIRTIO_IOMMU_F_MMIO
);
998 virtio_add_feature(&s
->features
, VIRTIO_IOMMU_F_PROBE
);
1000 qemu_mutex_init(&s
->mutex
);
1002 s
->as_by_busptr
= g_hash_table_new_full(NULL
, NULL
, NULL
, g_free
);
1004 if (s
->primary_bus
) {
1005 pci_setup_iommu(s
->primary_bus
, virtio_iommu_find_add_as
, s
);
1007 error_setg(errp
, "VIRTIO-IOMMU is not attached to any PCI bus!");
1011 static void virtio_iommu_device_unrealize(DeviceState
*dev
)
1013 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1014 VirtIOIOMMU
*s
= VIRTIO_IOMMU(dev
);
1016 g_hash_table_destroy(s
->as_by_busptr
);
1018 g_tree_destroy(s
->domains
);
1021 g_tree_destroy(s
->endpoints
);
1024 virtio_delete_queue(s
->req_vq
);
1025 virtio_delete_queue(s
->event_vq
);
1026 virtio_cleanup(vdev
);
1029 static void virtio_iommu_device_reset(VirtIODevice
*vdev
)
1031 VirtIOIOMMU
*s
= VIRTIO_IOMMU(vdev
);
1033 trace_virtio_iommu_device_reset();
1036 g_tree_destroy(s
->domains
);
1039 g_tree_destroy(s
->endpoints
);
1041 s
->domains
= g_tree_new_full((GCompareDataFunc
)int_cmp
,
1042 NULL
, NULL
, virtio_iommu_put_domain
);
1043 s
->endpoints
= g_tree_new_full((GCompareDataFunc
)int_cmp
,
1044 NULL
, NULL
, virtio_iommu_put_endpoint
);
1047 static void virtio_iommu_set_status(VirtIODevice
*vdev
, uint8_t status
)
1049 trace_virtio_iommu_device_status(status
);
1052 static void virtio_iommu_instance_init(Object
*obj
)
1056 #define VMSTATE_INTERVAL \
1058 .name = "interval", \
1060 .minimum_version_id = 1, \
1061 .fields = (VMStateField[]) { \
1062 VMSTATE_UINT64(low, VirtIOIOMMUInterval), \
1063 VMSTATE_UINT64(high, VirtIOIOMMUInterval), \
1064 VMSTATE_END_OF_LIST() \
1068 #define VMSTATE_MAPPING \
1070 .name = "mapping", \
1072 .minimum_version_id = 1, \
1073 .fields = (VMStateField[]) { \
1074 VMSTATE_UINT64(phys_addr, VirtIOIOMMUMapping),\
1075 VMSTATE_UINT32(flags, VirtIOIOMMUMapping), \
1076 VMSTATE_END_OF_LIST() \
1080 static const VMStateDescription vmstate_interval_mapping
[2] = {
1081 VMSTATE_MAPPING
, /* value */
1082 VMSTATE_INTERVAL
/* key */
1085 static int domain_preload(void *opaque
)
1087 VirtIOIOMMUDomain
*domain
= opaque
;
1089 domain
->mappings
= g_tree_new_full((GCompareDataFunc
)interval_cmp
,
1090 NULL
, g_free
, g_free
);
1094 static const VMStateDescription vmstate_endpoint
= {
1097 .minimum_version_id
= 1,
1098 .fields
= (VMStateField
[]) {
1099 VMSTATE_UINT32(id
, VirtIOIOMMUEndpoint
),
1100 VMSTATE_END_OF_LIST()
1104 static const VMStateDescription vmstate_domain
= {
1107 .minimum_version_id
= 1,
1108 .pre_load
= domain_preload
,
1109 .fields
= (VMStateField
[]) {
1110 VMSTATE_UINT32(id
, VirtIOIOMMUDomain
),
1111 VMSTATE_GTREE_V(mappings
, VirtIOIOMMUDomain
, 1,
1112 vmstate_interval_mapping
,
1113 VirtIOIOMMUInterval
, VirtIOIOMMUMapping
),
1114 VMSTATE_QLIST_V(endpoint_list
, VirtIOIOMMUDomain
, 1,
1115 vmstate_endpoint
, VirtIOIOMMUEndpoint
, next
),
1116 VMSTATE_END_OF_LIST()
1120 static gboolean
reconstruct_endpoints(gpointer key
, gpointer value
,
1123 VirtIOIOMMU
*s
= (VirtIOIOMMU
*)data
;
1124 VirtIOIOMMUDomain
*d
= (VirtIOIOMMUDomain
*)value
;
1125 VirtIOIOMMUEndpoint
*iter
;
1126 IOMMUMemoryRegion
*mr
;
1128 QLIST_FOREACH(iter
, &d
->endpoint_list
, next
) {
1129 mr
= virtio_iommu_mr(s
, iter
->id
);
1133 iter
->iommu_mr
= mr
;
1134 g_tree_insert(s
->endpoints
, GUINT_TO_POINTER(iter
->id
), iter
);
1136 return false; /* continue the domain traversal */
1139 static int iommu_post_load(void *opaque
, int version_id
)
1141 VirtIOIOMMU
*s
= opaque
;
1143 g_tree_foreach(s
->domains
, reconstruct_endpoints
, s
);
1147 static const VMStateDescription vmstate_virtio_iommu_device
= {
1148 .name
= "virtio-iommu-device",
1149 .minimum_version_id
= 1,
1151 .post_load
= iommu_post_load
,
1152 .fields
= (VMStateField
[]) {
1153 VMSTATE_GTREE_DIRECT_KEY_V(domains
, VirtIOIOMMU
, 1,
1154 &vmstate_domain
, VirtIOIOMMUDomain
),
1155 VMSTATE_END_OF_LIST()
1159 static const VMStateDescription vmstate_virtio_iommu
= {
1160 .name
= "virtio-iommu",
1161 .minimum_version_id
= 1,
1162 .priority
= MIG_PRI_IOMMU
,
1164 .fields
= (VMStateField
[]) {
1165 VMSTATE_VIRTIO_DEVICE
,
1166 VMSTATE_END_OF_LIST()
1170 static Property virtio_iommu_properties
[] = {
1171 DEFINE_PROP_LINK("primary-bus", VirtIOIOMMU
, primary_bus
, "PCI", PCIBus
*),
1172 DEFINE_PROP_END_OF_LIST(),
1175 static void virtio_iommu_class_init(ObjectClass
*klass
, void *data
)
1177 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1178 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1180 device_class_set_props(dc
, virtio_iommu_properties
);
1181 dc
->vmsd
= &vmstate_virtio_iommu
;
1183 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1184 vdc
->realize
= virtio_iommu_device_realize
;
1185 vdc
->unrealize
= virtio_iommu_device_unrealize
;
1186 vdc
->reset
= virtio_iommu_device_reset
;
1187 vdc
->get_config
= virtio_iommu_get_config
;
1188 vdc
->set_config
= virtio_iommu_set_config
;
1189 vdc
->get_features
= virtio_iommu_get_features
;
1190 vdc
->set_status
= virtio_iommu_set_status
;
1191 vdc
->vmsd
= &vmstate_virtio_iommu_device
;
1194 static void virtio_iommu_memory_region_class_init(ObjectClass
*klass
,
1197 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
1199 imrc
->translate
= virtio_iommu_translate
;
1200 imrc
->replay
= virtio_iommu_replay
;
1201 imrc
->notify_flag_changed
= virtio_iommu_notify_flag_changed
;
1202 imrc
->iommu_set_page_size_mask
= virtio_iommu_set_page_size_mask
;
1205 static const TypeInfo virtio_iommu_info
= {
1206 .name
= TYPE_VIRTIO_IOMMU
,
1207 .parent
= TYPE_VIRTIO_DEVICE
,
1208 .instance_size
= sizeof(VirtIOIOMMU
),
1209 .instance_init
= virtio_iommu_instance_init
,
1210 .class_init
= virtio_iommu_class_init
,
1213 static const TypeInfo virtio_iommu_memory_region_info
= {
1214 .parent
= TYPE_IOMMU_MEMORY_REGION
,
1215 .name
= TYPE_VIRTIO_IOMMU_MEMORY_REGION
,
1216 .class_init
= virtio_iommu_memory_region_class_init
,
1219 static void virtio_register_types(void)
1221 type_register_static(&virtio_iommu_info
);
1222 type_register_static(&virtio_iommu_memory_region_info
);
1225 type_init(virtio_register_types
)