1 // SPDX-License-Identifier: GPL-2.0
3 * Virtio driver for the paravirtualized IOMMU
5 * Copyright (C) 2019 Arm Limited
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/delay.h>
11 #include <linux/dma-map-ops.h>
12 #include <linux/freezer.h>
13 #include <linux/interval_tree.h>
14 #include <linux/iommu.h>
15 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/virtio.h>
19 #include <linux/virtio_config.h>
20 #include <linux/virtio_ids.h>
21 #include <linux/wait.h>
23 #include <uapi/linux/virtio_iommu.h>
25 #include "dma-iommu.h"
27 #define MSI_IOVA_BASE 0x8000000
28 #define MSI_IOVA_LENGTH 0x100000
30 #define VIOMMU_REQUEST_VQ 0
31 #define VIOMMU_EVENT_VQ 1
32 #define VIOMMU_NR_VQS 2
35 struct iommu_device iommu
;
37 struct virtio_device
*vdev
;
39 struct ida domain_ids
;
41 struct virtqueue
*vqs
[VIOMMU_NR_VQS
];
42 spinlock_t request_lock
;
43 struct list_head requests
;
46 /* Device configuration */
47 struct iommu_domain_geometry geometry
;
51 /* Supported MAP flags */
56 struct viommu_mapping
{
58 struct interval_tree_node iova
;
62 struct viommu_domain
{
63 struct iommu_domain domain
;
64 struct viommu_dev
*viommu
;
65 struct mutex mutex
; /* protects viommu pointer */
69 spinlock_t mappings_lock
;
70 struct rb_root_cached mappings
;
72 unsigned long nr_endpoints
;
76 struct viommu_endpoint
{
78 struct viommu_dev
*viommu
;
79 struct viommu_domain
*vdomain
;
80 struct list_head resv_regions
;
83 struct viommu_request
{
84 struct list_head list
;
86 unsigned int write_offset
;
88 char buf
[] __counted_by(len
);
91 #define VIOMMU_FAULT_RESV_MASK 0xffffff00
96 struct virtio_iommu_fault fault
;
100 #define to_viommu_domain(domain) \
101 container_of(domain, struct viommu_domain, domain)
103 static int viommu_get_req_errno(void *buf
, size_t len
)
105 struct virtio_iommu_req_tail
*tail
= buf
+ len
- sizeof(*tail
);
107 switch (tail
->status
) {
108 case VIRTIO_IOMMU_S_OK
:
110 case VIRTIO_IOMMU_S_UNSUPP
:
112 case VIRTIO_IOMMU_S_INVAL
:
114 case VIRTIO_IOMMU_S_RANGE
:
116 case VIRTIO_IOMMU_S_NOENT
:
118 case VIRTIO_IOMMU_S_FAULT
:
120 case VIRTIO_IOMMU_S_NOMEM
:
122 case VIRTIO_IOMMU_S_IOERR
:
123 case VIRTIO_IOMMU_S_DEVERR
:
129 static void viommu_set_req_status(void *buf
, size_t len
, int status
)
131 struct virtio_iommu_req_tail
*tail
= buf
+ len
- sizeof(*tail
);
133 tail
->status
= status
;
136 static off_t
viommu_get_write_desc_offset(struct viommu_dev
*viommu
,
137 struct virtio_iommu_req_head
*req
,
140 size_t tail_size
= sizeof(struct virtio_iommu_req_tail
);
142 if (req
->type
== VIRTIO_IOMMU_T_PROBE
)
143 return len
- viommu
->probe_size
- tail_size
;
145 return len
- tail_size
;
149 * __viommu_sync_req - Complete all in-flight requests
151 * Wait for all added requests to complete. When this function returns, all
152 * requests that were in-flight at the time of the call have completed.
154 static int __viommu_sync_req(struct viommu_dev
*viommu
)
158 struct viommu_request
*req
;
159 struct virtqueue
*vq
= viommu
->vqs
[VIOMMU_REQUEST_VQ
];
161 assert_spin_locked(&viommu
->request_lock
);
165 while (!list_empty(&viommu
->requests
)) {
167 req
= virtqueue_get_buf(vq
, &len
);
172 viommu_set_req_status(req
->buf
, req
->len
,
173 VIRTIO_IOMMU_S_IOERR
);
175 write_len
= req
->len
- req
->write_offset
;
176 if (req
->writeback
&& len
== write_len
)
177 memcpy(req
->writeback
, req
->buf
+ req
->write_offset
,
180 list_del(&req
->list
);
187 static int viommu_sync_req(struct viommu_dev
*viommu
)
192 spin_lock_irqsave(&viommu
->request_lock
, flags
);
193 ret
= __viommu_sync_req(viommu
);
195 dev_dbg(viommu
->dev
, "could not sync requests (%d)\n", ret
);
196 spin_unlock_irqrestore(&viommu
->request_lock
, flags
);
202 * __viommu_add_request - Add one request to the queue
203 * @buf: pointer to the request buffer
204 * @len: length of the request buffer
205 * @writeback: copy data back to the buffer when the request completes.
207 * Add a request to the queue. Only synchronize the queue if it's already full.
208 * Otherwise don't kick the queue nor wait for requests to complete.
210 * When @writeback is true, data written by the device, including the request
211 * status, is copied into @buf after the request completes. This is unsafe if
212 * the caller allocates @buf on stack and drops the lock between add_req() and
215 * Return 0 if the request was successfully added to the queue.
217 static int __viommu_add_req(struct viommu_dev
*viommu
, void *buf
, size_t len
,
222 struct viommu_request
*req
;
223 struct scatterlist top_sg
, bottom_sg
;
224 struct scatterlist
*sg
[2] = { &top_sg
, &bottom_sg
};
225 struct virtqueue
*vq
= viommu
->vqs
[VIOMMU_REQUEST_VQ
];
227 assert_spin_locked(&viommu
->request_lock
);
229 write_offset
= viommu_get_write_desc_offset(viommu
, buf
, len
);
230 if (write_offset
<= 0)
233 req
= kzalloc(struct_size(req
, buf
, len
), GFP_ATOMIC
);
239 req
->writeback
= buf
+ write_offset
;
240 req
->write_offset
= write_offset
;
242 memcpy(&req
->buf
, buf
, write_offset
);
244 sg_init_one(&top_sg
, req
->buf
, write_offset
);
245 sg_init_one(&bottom_sg
, req
->buf
+ write_offset
, len
- write_offset
);
247 ret
= virtqueue_add_sgs(vq
, sg
, 1, 1, req
, GFP_ATOMIC
);
248 if (ret
== -ENOSPC
) {
249 /* If the queue is full, sync and retry */
250 if (!__viommu_sync_req(viommu
))
251 ret
= virtqueue_add_sgs(vq
, sg
, 1, 1, req
, GFP_ATOMIC
);
256 list_add_tail(&req
->list
, &viommu
->requests
);
264 static int viommu_add_req(struct viommu_dev
*viommu
, void *buf
, size_t len
)
269 spin_lock_irqsave(&viommu
->request_lock
, flags
);
270 ret
= __viommu_add_req(viommu
, buf
, len
, false);
272 dev_dbg(viommu
->dev
, "could not add request: %d\n", ret
);
273 spin_unlock_irqrestore(&viommu
->request_lock
, flags
);
279 * Send a request and wait for it to complete. Return the request status (as an
282 static int viommu_send_req_sync(struct viommu_dev
*viommu
, void *buf
,
288 spin_lock_irqsave(&viommu
->request_lock
, flags
);
290 ret
= __viommu_add_req(viommu
, buf
, len
, true);
292 dev_dbg(viommu
->dev
, "could not add request (%d)\n", ret
);
296 ret
= __viommu_sync_req(viommu
);
298 dev_dbg(viommu
->dev
, "could not sync requests (%d)\n", ret
);
299 /* Fall-through (get the actual request status) */
302 ret
= viommu_get_req_errno(buf
, len
);
304 spin_unlock_irqrestore(&viommu
->request_lock
, flags
);
309 * viommu_add_mapping - add a mapping to the internal tree
311 * On success, return the new mapping. Otherwise return NULL.
313 static int viommu_add_mapping(struct viommu_domain
*vdomain
, u64 iova
, u64 end
,
314 phys_addr_t paddr
, u32 flags
)
316 unsigned long irqflags
;
317 struct viommu_mapping
*mapping
;
319 mapping
= kzalloc(sizeof(*mapping
), GFP_ATOMIC
);
323 mapping
->paddr
= paddr
;
324 mapping
->iova
.start
= iova
;
325 mapping
->iova
.last
= end
;
326 mapping
->flags
= flags
;
328 spin_lock_irqsave(&vdomain
->mappings_lock
, irqflags
);
329 interval_tree_insert(&mapping
->iova
, &vdomain
->mappings
);
330 spin_unlock_irqrestore(&vdomain
->mappings_lock
, irqflags
);
336 * viommu_del_mappings - remove mappings from the internal tree
338 * @vdomain: the domain
339 * @iova: start of the range
340 * @end: end of the range
342 * On success, returns the number of unmapped bytes
344 static size_t viommu_del_mappings(struct viommu_domain
*vdomain
,
349 struct viommu_mapping
*mapping
= NULL
;
350 struct interval_tree_node
*node
, *next
;
352 spin_lock_irqsave(&vdomain
->mappings_lock
, flags
);
353 next
= interval_tree_iter_first(&vdomain
->mappings
, iova
, end
);
356 mapping
= container_of(node
, struct viommu_mapping
, iova
);
357 next
= interval_tree_iter_next(node
, iova
, end
);
359 /* Trying to split a mapping? */
360 if (mapping
->iova
.start
< iova
)
364 * Virtio-iommu doesn't allow UNMAP to split a mapping created
365 * with a single MAP request, so remove the full mapping.
367 unmapped
+= mapping
->iova
.last
- mapping
->iova
.start
+ 1;
369 interval_tree_remove(node
, &vdomain
->mappings
);
372 spin_unlock_irqrestore(&vdomain
->mappings_lock
, flags
);
378 * Fill the domain with identity mappings, skipping the device's reserved
381 static int viommu_domain_map_identity(struct viommu_endpoint
*vdev
,
382 struct viommu_domain
*vdomain
)
385 struct iommu_resv_region
*resv
;
386 u64 iova
= vdomain
->domain
.geometry
.aperture_start
;
387 u64 limit
= vdomain
->domain
.geometry
.aperture_end
;
388 u32 flags
= VIRTIO_IOMMU_MAP_F_READ
| VIRTIO_IOMMU_MAP_F_WRITE
;
389 unsigned long granule
= 1UL << __ffs(vdomain
->domain
.pgsize_bitmap
);
391 iova
= ALIGN(iova
, granule
);
392 limit
= ALIGN_DOWN(limit
+ 1, granule
) - 1;
394 list_for_each_entry(resv
, &vdev
->resv_regions
, list
) {
395 u64 resv_start
= ALIGN_DOWN(resv
->start
, granule
);
396 u64 resv_end
= ALIGN(resv
->start
+ resv
->length
, granule
) - 1;
398 if (resv_end
< iova
|| resv_start
> limit
)
402 if (resv_start
> iova
) {
403 ret
= viommu_add_mapping(vdomain
, iova
, resv_start
- 1,
404 (phys_addr_t
)iova
, flags
);
409 if (resv_end
>= limit
)
415 ret
= viommu_add_mapping(vdomain
, iova
, limit
, (phys_addr_t
)iova
,
422 viommu_del_mappings(vdomain
, 0, iova
);
427 * viommu_replay_mappings - re-send MAP requests
429 * When reattaching a domain that was previously detached from all endpoints,
430 * mappings were deleted from the device. Re-create the mappings available in
433 static int viommu_replay_mappings(struct viommu_domain
*vdomain
)
437 struct viommu_mapping
*mapping
;
438 struct interval_tree_node
*node
;
439 struct virtio_iommu_req_map map
;
441 spin_lock_irqsave(&vdomain
->mappings_lock
, flags
);
442 node
= interval_tree_iter_first(&vdomain
->mappings
, 0, -1UL);
444 mapping
= container_of(node
, struct viommu_mapping
, iova
);
445 map
= (struct virtio_iommu_req_map
) {
446 .head
.type
= VIRTIO_IOMMU_T_MAP
,
447 .domain
= cpu_to_le32(vdomain
->id
),
448 .virt_start
= cpu_to_le64(mapping
->iova
.start
),
449 .virt_end
= cpu_to_le64(mapping
->iova
.last
),
450 .phys_start
= cpu_to_le64(mapping
->paddr
),
451 .flags
= cpu_to_le32(mapping
->flags
),
454 ret
= viommu_send_req_sync(vdomain
->viommu
, &map
, sizeof(map
));
458 node
= interval_tree_iter_next(node
, 0, -1UL);
460 spin_unlock_irqrestore(&vdomain
->mappings_lock
, flags
);
465 static int viommu_add_resv_mem(struct viommu_endpoint
*vdev
,
466 struct virtio_iommu_probe_resv_mem
*mem
,
471 phys_addr_t start
, end
;
472 struct iommu_resv_region
*region
= NULL
, *next
;
473 unsigned long prot
= IOMMU_WRITE
| IOMMU_NOEXEC
| IOMMU_MMIO
;
475 start
= start64
= le64_to_cpu(mem
->start
);
476 end
= end64
= le64_to_cpu(mem
->end
);
477 size
= end64
- start64
+ 1;
479 /* Catch any overflow, including the unlikely end64 - start64 + 1 = 0 */
480 if (start
!= start64
|| end
!= end64
|| size
< end64
- start64
)
483 if (len
< sizeof(*mem
))
486 switch (mem
->subtype
) {
488 dev_warn(vdev
->dev
, "unknown resv mem subtype 0x%x\n",
491 case VIRTIO_IOMMU_RESV_MEM_T_RESERVED
:
492 region
= iommu_alloc_resv_region(start
, size
, 0,
496 case VIRTIO_IOMMU_RESV_MEM_T_MSI
:
497 region
= iommu_alloc_resv_region(start
, size
, prot
,
505 /* Keep the list sorted */
506 list_for_each_entry(next
, &vdev
->resv_regions
, list
) {
507 if (next
->start
> region
->start
)
510 list_add_tail(®ion
->list
, &next
->list
);
514 static int viommu_probe_endpoint(struct viommu_dev
*viommu
, struct device
*dev
)
520 struct virtio_iommu_req_probe
*probe
;
521 struct virtio_iommu_probe_property
*prop
;
522 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
523 struct viommu_endpoint
*vdev
= dev_iommu_priv_get(dev
);
525 if (!fwspec
->num_ids
)
528 probe_len
= sizeof(*probe
) + viommu
->probe_size
+
529 sizeof(struct virtio_iommu_req_tail
);
530 probe
= kzalloc(probe_len
, GFP_KERNEL
);
534 probe
->head
.type
= VIRTIO_IOMMU_T_PROBE
;
536 * For now, assume that properties of an endpoint that outputs multiple
537 * IDs are consistent. Only probe the first one.
539 probe
->endpoint
= cpu_to_le32(fwspec
->ids
[0]);
541 ret
= viommu_send_req_sync(viommu
, probe
, probe_len
);
545 prop
= (void *)probe
->properties
;
546 type
= le16_to_cpu(prop
->type
) & VIRTIO_IOMMU_PROBE_T_MASK
;
548 while (type
!= VIRTIO_IOMMU_PROBE_T_NONE
&&
549 cur
< viommu
->probe_size
) {
550 len
= le16_to_cpu(prop
->length
) + sizeof(*prop
);
553 case VIRTIO_IOMMU_PROBE_T_RESV_MEM
:
554 ret
= viommu_add_resv_mem(vdev
, (void *)prop
, len
);
557 dev_err(dev
, "unknown viommu prop 0x%x\n", type
);
561 dev_err(dev
, "failed to parse viommu prop 0x%x\n", type
);
564 if (cur
>= viommu
->probe_size
)
567 prop
= (void *)probe
->properties
+ cur
;
568 type
= le16_to_cpu(prop
->type
) & VIRTIO_IOMMU_PROBE_T_MASK
;
576 static int viommu_fault_handler(struct viommu_dev
*viommu
,
577 struct virtio_iommu_fault
*fault
)
581 u8 reason
= fault
->reason
;
582 u32 flags
= le32_to_cpu(fault
->flags
);
583 u32 endpoint
= le32_to_cpu(fault
->endpoint
);
584 u64 address
= le64_to_cpu(fault
->address
);
587 case VIRTIO_IOMMU_FAULT_R_DOMAIN
:
588 reason_str
= "domain";
590 case VIRTIO_IOMMU_FAULT_R_MAPPING
:
593 case VIRTIO_IOMMU_FAULT_R_UNKNOWN
:
595 reason_str
= "unknown";
599 /* TODO: find EP by ID and report_iommu_fault */
600 if (flags
& VIRTIO_IOMMU_FAULT_F_ADDRESS
)
601 dev_err_ratelimited(viommu
->dev
, "%s fault from EP %u at %#llx [%s%s%s]\n",
602 reason_str
, endpoint
, address
,
603 flags
& VIRTIO_IOMMU_FAULT_F_READ
? "R" : "",
604 flags
& VIRTIO_IOMMU_FAULT_F_WRITE
? "W" : "",
605 flags
& VIRTIO_IOMMU_FAULT_F_EXEC
? "X" : "");
607 dev_err_ratelimited(viommu
->dev
, "%s fault from EP %u\n",
608 reason_str
, endpoint
);
612 static void viommu_event_handler(struct virtqueue
*vq
)
616 struct scatterlist sg
[1];
617 struct viommu_event
*evt
;
618 struct viommu_dev
*viommu
= vq
->vdev
->priv
;
620 while ((evt
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
621 if (len
> sizeof(*evt
)) {
623 "invalid event buffer (len %u != %zu)\n",
625 } else if (!(evt
->head
& VIOMMU_FAULT_RESV_MASK
)) {
626 viommu_fault_handler(viommu
, &evt
->fault
);
629 sg_init_one(sg
, evt
, sizeof(*evt
));
630 ret
= virtqueue_add_inbuf(vq
, sg
, 1, evt
, GFP_ATOMIC
);
632 dev_err(viommu
->dev
, "could not add event buffer\n");
640 static struct iommu_domain
*viommu_domain_alloc(unsigned type
)
642 struct viommu_domain
*vdomain
;
644 if (type
!= IOMMU_DOMAIN_UNMANAGED
&&
645 type
!= IOMMU_DOMAIN_DMA
&&
646 type
!= IOMMU_DOMAIN_IDENTITY
)
649 vdomain
= kzalloc(sizeof(*vdomain
), GFP_KERNEL
);
653 mutex_init(&vdomain
->mutex
);
654 spin_lock_init(&vdomain
->mappings_lock
);
655 vdomain
->mappings
= RB_ROOT_CACHED
;
657 return &vdomain
->domain
;
660 static int viommu_domain_finalise(struct viommu_endpoint
*vdev
,
661 struct iommu_domain
*domain
)
664 unsigned long viommu_page_size
;
665 struct viommu_dev
*viommu
= vdev
->viommu
;
666 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
668 viommu_page_size
= 1UL << __ffs(viommu
->pgsize_bitmap
);
669 if (viommu_page_size
> PAGE_SIZE
) {
671 "granule 0x%lx larger than system page size 0x%lx\n",
672 viommu_page_size
, PAGE_SIZE
);
676 ret
= ida_alloc_range(&viommu
->domain_ids
, viommu
->first_domain
,
677 viommu
->last_domain
, GFP_KERNEL
);
681 vdomain
->id
= (unsigned int)ret
;
683 domain
->pgsize_bitmap
= viommu
->pgsize_bitmap
;
684 domain
->geometry
= viommu
->geometry
;
686 vdomain
->map_flags
= viommu
->map_flags
;
687 vdomain
->viommu
= viommu
;
689 if (domain
->type
== IOMMU_DOMAIN_IDENTITY
) {
690 if (virtio_has_feature(viommu
->vdev
,
691 VIRTIO_IOMMU_F_BYPASS_CONFIG
)) {
692 vdomain
->bypass
= true;
696 ret
= viommu_domain_map_identity(vdev
, vdomain
);
698 ida_free(&viommu
->domain_ids
, vdomain
->id
);
699 vdomain
->viommu
= NULL
;
707 static void viommu_domain_free(struct iommu_domain
*domain
)
709 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
711 /* Free all remaining mappings */
712 viommu_del_mappings(vdomain
, 0, ULLONG_MAX
);
715 ida_free(&vdomain
->viommu
->domain_ids
, vdomain
->id
);
720 static int viommu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
724 struct virtio_iommu_req_attach req
;
725 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
726 struct viommu_endpoint
*vdev
= dev_iommu_priv_get(dev
);
727 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
729 mutex_lock(&vdomain
->mutex
);
730 if (!vdomain
->viommu
) {
732 * Properly initialize the domain now that we know which viommu
735 ret
= viommu_domain_finalise(vdev
, domain
);
736 } else if (vdomain
->viommu
!= vdev
->viommu
) {
739 mutex_unlock(&vdomain
->mutex
);
745 * In the virtio-iommu device, when attaching the endpoint to a new
746 * domain, it is detached from the old one and, if as a result the
747 * old domain isn't attached to any endpoint, all mappings are removed
748 * from the old domain and it is freed.
750 * In the driver the old domain still exists, and its mappings will be
751 * recreated if it gets reattached to an endpoint. Otherwise it will be
754 * vdev->vdomain is protected by group->mutex
757 vdev
->vdomain
->nr_endpoints
--;
759 req
= (struct virtio_iommu_req_attach
) {
760 .head
.type
= VIRTIO_IOMMU_T_ATTACH
,
761 .domain
= cpu_to_le32(vdomain
->id
),
765 req
.flags
|= cpu_to_le32(VIRTIO_IOMMU_ATTACH_F_BYPASS
);
767 for (i
= 0; i
< fwspec
->num_ids
; i
++) {
768 req
.endpoint
= cpu_to_le32(fwspec
->ids
[i
]);
770 ret
= viommu_send_req_sync(vdomain
->viommu
, &req
, sizeof(req
));
775 if (!vdomain
->nr_endpoints
) {
777 * This endpoint is the first to be attached to the domain.
778 * Replay existing mappings (e.g. SW MSI).
780 ret
= viommu_replay_mappings(vdomain
);
785 vdomain
->nr_endpoints
++;
786 vdev
->vdomain
= vdomain
;
791 static void viommu_detach_dev(struct viommu_endpoint
*vdev
)
794 struct virtio_iommu_req_detach req
;
795 struct viommu_domain
*vdomain
= vdev
->vdomain
;
796 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(vdev
->dev
);
801 req
= (struct virtio_iommu_req_detach
) {
802 .head
.type
= VIRTIO_IOMMU_T_DETACH
,
803 .domain
= cpu_to_le32(vdomain
->id
),
806 for (i
= 0; i
< fwspec
->num_ids
; i
++) {
807 req
.endpoint
= cpu_to_le32(fwspec
->ids
[i
]);
808 WARN_ON(viommu_send_req_sync(vdev
->viommu
, &req
, sizeof(req
)));
810 vdomain
->nr_endpoints
--;
811 vdev
->vdomain
= NULL
;
814 static int viommu_map_pages(struct iommu_domain
*domain
, unsigned long iova
,
815 phys_addr_t paddr
, size_t pgsize
, size_t pgcount
,
816 int prot
, gfp_t gfp
, size_t *mapped
)
820 size_t size
= pgsize
* pgcount
;
821 u64 end
= iova
+ size
- 1;
822 struct virtio_iommu_req_map map
;
823 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
825 flags
= (prot
& IOMMU_READ
? VIRTIO_IOMMU_MAP_F_READ
: 0) |
826 (prot
& IOMMU_WRITE
? VIRTIO_IOMMU_MAP_F_WRITE
: 0) |
827 (prot
& IOMMU_MMIO
? VIRTIO_IOMMU_MAP_F_MMIO
: 0);
829 if (flags
& ~vdomain
->map_flags
)
832 ret
= viommu_add_mapping(vdomain
, iova
, end
, paddr
, flags
);
836 if (vdomain
->nr_endpoints
) {
837 map
= (struct virtio_iommu_req_map
) {
838 .head
.type
= VIRTIO_IOMMU_T_MAP
,
839 .domain
= cpu_to_le32(vdomain
->id
),
840 .virt_start
= cpu_to_le64(iova
),
841 .phys_start
= cpu_to_le64(paddr
),
842 .virt_end
= cpu_to_le64(end
),
843 .flags
= cpu_to_le32(flags
),
846 ret
= viommu_add_req(vdomain
->viommu
, &map
, sizeof(map
));
848 viommu_del_mappings(vdomain
, iova
, end
);
858 static size_t viommu_unmap_pages(struct iommu_domain
*domain
, unsigned long iova
,
859 size_t pgsize
, size_t pgcount
,
860 struct iommu_iotlb_gather
*gather
)
864 struct virtio_iommu_req_unmap unmap
;
865 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
866 size_t size
= pgsize
* pgcount
;
868 unmapped
= viommu_del_mappings(vdomain
, iova
, iova
+ size
- 1);
872 /* Device already removed all mappings after detach. */
873 if (!vdomain
->nr_endpoints
)
876 unmap
= (struct virtio_iommu_req_unmap
) {
877 .head
.type
= VIRTIO_IOMMU_T_UNMAP
,
878 .domain
= cpu_to_le32(vdomain
->id
),
879 .virt_start
= cpu_to_le64(iova
),
880 .virt_end
= cpu_to_le64(iova
+ unmapped
- 1),
883 ret
= viommu_add_req(vdomain
->viommu
, &unmap
, sizeof(unmap
));
884 return ret
? 0 : unmapped
;
887 static phys_addr_t
viommu_iova_to_phys(struct iommu_domain
*domain
,
892 struct viommu_mapping
*mapping
;
893 struct interval_tree_node
*node
;
894 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
896 spin_lock_irqsave(&vdomain
->mappings_lock
, flags
);
897 node
= interval_tree_iter_first(&vdomain
->mappings
, iova
, iova
);
899 mapping
= container_of(node
, struct viommu_mapping
, iova
);
900 paddr
= mapping
->paddr
+ (iova
- mapping
->iova
.start
);
902 spin_unlock_irqrestore(&vdomain
->mappings_lock
, flags
);
907 static void viommu_iotlb_sync(struct iommu_domain
*domain
,
908 struct iommu_iotlb_gather
*gather
)
910 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
912 viommu_sync_req(vdomain
->viommu
);
915 static int viommu_iotlb_sync_map(struct iommu_domain
*domain
,
916 unsigned long iova
, size_t size
)
918 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
921 * May be called before the viommu is initialized including
922 * while creating direct mapping
924 if (!vdomain
->nr_endpoints
)
926 return viommu_sync_req(vdomain
->viommu
);
929 static void viommu_flush_iotlb_all(struct iommu_domain
*domain
)
931 struct viommu_domain
*vdomain
= to_viommu_domain(domain
);
934 * May be called before the viommu is initialized including
935 * while creating direct mapping
937 if (!vdomain
->nr_endpoints
)
939 viommu_sync_req(vdomain
->viommu
);
942 static void viommu_get_resv_regions(struct device
*dev
, struct list_head
*head
)
944 struct iommu_resv_region
*entry
, *new_entry
, *msi
= NULL
;
945 struct viommu_endpoint
*vdev
= dev_iommu_priv_get(dev
);
946 int prot
= IOMMU_WRITE
| IOMMU_NOEXEC
| IOMMU_MMIO
;
948 list_for_each_entry(entry
, &vdev
->resv_regions
, list
) {
949 if (entry
->type
== IOMMU_RESV_MSI
)
952 new_entry
= kmemdup(entry
, sizeof(*entry
), GFP_KERNEL
);
955 list_add_tail(&new_entry
->list
, head
);
959 * If the device didn't register any bypass MSI window, add a
960 * software-mapped region.
963 msi
= iommu_alloc_resv_region(MSI_IOVA_BASE
, MSI_IOVA_LENGTH
,
964 prot
, IOMMU_RESV_SW_MSI
,
969 list_add_tail(&msi
->list
, head
);
972 iommu_dma_get_resv_regions(dev
, head
);
975 static struct iommu_ops viommu_ops
;
976 static struct virtio_driver virtio_iommu_drv
;
978 static int viommu_match_node(struct device
*dev
, const void *data
)
980 return device_match_fwnode(dev
->parent
, data
);
983 static struct viommu_dev
*viommu_get_by_fwnode(struct fwnode_handle
*fwnode
)
985 struct device
*dev
= driver_find_device(&virtio_iommu_drv
.driver
, NULL
,
986 fwnode
, viommu_match_node
);
989 return dev
? dev_to_virtio(dev
)->priv
: NULL
;
992 static struct iommu_device
*viommu_probe_device(struct device
*dev
)
995 struct viommu_endpoint
*vdev
;
996 struct viommu_dev
*viommu
= NULL
;
997 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
999 viommu
= viommu_get_by_fwnode(fwspec
->iommu_fwnode
);
1001 return ERR_PTR(-ENODEV
);
1003 vdev
= kzalloc(sizeof(*vdev
), GFP_KERNEL
);
1005 return ERR_PTR(-ENOMEM
);
1008 vdev
->viommu
= viommu
;
1009 INIT_LIST_HEAD(&vdev
->resv_regions
);
1010 dev_iommu_priv_set(dev
, vdev
);
1012 if (viommu
->probe_size
) {
1013 /* Get additional information for this endpoint */
1014 ret
= viommu_probe_endpoint(viommu
, dev
);
1019 return &viommu
->iommu
;
1022 iommu_put_resv_regions(dev
, &vdev
->resv_regions
);
1025 return ERR_PTR(ret
);
1028 static void viommu_release_device(struct device
*dev
)
1030 struct viommu_endpoint
*vdev
= dev_iommu_priv_get(dev
);
1032 viommu_detach_dev(vdev
);
1033 iommu_put_resv_regions(dev
, &vdev
->resv_regions
);
1037 static struct iommu_group
*viommu_device_group(struct device
*dev
)
1039 if (dev_is_pci(dev
))
1040 return pci_device_group(dev
);
1042 return generic_device_group(dev
);
1045 static int viommu_of_xlate(struct device
*dev
,
1046 const struct of_phandle_args
*args
)
1048 return iommu_fwspec_add_ids(dev
, args
->args
, 1);
1051 static bool viommu_capable(struct device
*dev
, enum iommu_cap cap
)
1054 case IOMMU_CAP_CACHE_COHERENCY
:
1056 case IOMMU_CAP_DEFERRED_FLUSH
:
1063 static struct iommu_ops viommu_ops
= {
1064 .capable
= viommu_capable
,
1065 .domain_alloc
= viommu_domain_alloc
,
1066 .probe_device
= viommu_probe_device
,
1067 .release_device
= viommu_release_device
,
1068 .device_group
= viommu_device_group
,
1069 .get_resv_regions
= viommu_get_resv_regions
,
1070 .of_xlate
= viommu_of_xlate
,
1071 .owner
= THIS_MODULE
,
1072 .default_domain_ops
= &(const struct iommu_domain_ops
) {
1073 .attach_dev
= viommu_attach_dev
,
1074 .map_pages
= viommu_map_pages
,
1075 .unmap_pages
= viommu_unmap_pages
,
1076 .iova_to_phys
= viommu_iova_to_phys
,
1077 .flush_iotlb_all
= viommu_flush_iotlb_all
,
1078 .iotlb_sync
= viommu_iotlb_sync
,
1079 .iotlb_sync_map
= viommu_iotlb_sync_map
,
1080 .free
= viommu_domain_free
,
1084 static int viommu_init_vqs(struct viommu_dev
*viommu
)
1086 struct virtio_device
*vdev
= dev_to_virtio(viommu
->dev
);
1087 struct virtqueue_info vqs_info
[] = {
1089 { "event", viommu_event_handler
},
1092 return virtio_find_vqs(vdev
, VIOMMU_NR_VQS
, viommu
->vqs
,
1096 static int viommu_fill_evtq(struct viommu_dev
*viommu
)
1099 struct scatterlist sg
[1];
1100 struct viommu_event
*evts
;
1101 struct virtqueue
*vq
= viommu
->vqs
[VIOMMU_EVENT_VQ
];
1102 size_t nr_evts
= vq
->num_free
;
1104 viommu
->evts
= evts
= devm_kmalloc_array(viommu
->dev
, nr_evts
,
1105 sizeof(*evts
), GFP_KERNEL
);
1109 for (i
= 0; i
< nr_evts
; i
++) {
1110 sg_init_one(sg
, &evts
[i
], sizeof(*evts
));
1111 ret
= virtqueue_add_inbuf(vq
, sg
, 1, &evts
[i
], GFP_KERNEL
);
1119 static int viommu_probe(struct virtio_device
*vdev
)
1121 struct device
*parent_dev
= vdev
->dev
.parent
;
1122 struct viommu_dev
*viommu
= NULL
;
1123 struct device
*dev
= &vdev
->dev
;
1124 u64 input_start
= 0;
1125 u64 input_end
= -1UL;
1128 if (!virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
) ||
1129 !virtio_has_feature(vdev
, VIRTIO_IOMMU_F_MAP_UNMAP
))
1132 viommu
= devm_kzalloc(dev
, sizeof(*viommu
), GFP_KERNEL
);
1136 spin_lock_init(&viommu
->request_lock
);
1137 ida_init(&viommu
->domain_ids
);
1139 viommu
->vdev
= vdev
;
1140 INIT_LIST_HEAD(&viommu
->requests
);
1142 ret
= viommu_init_vqs(viommu
);
1146 virtio_cread_le(vdev
, struct virtio_iommu_config
, page_size_mask
,
1147 &viommu
->pgsize_bitmap
);
1149 if (!viommu
->pgsize_bitmap
) {
1154 viommu
->map_flags
= VIRTIO_IOMMU_MAP_F_READ
| VIRTIO_IOMMU_MAP_F_WRITE
;
1155 viommu
->last_domain
= ~0U;
1157 /* Optional features */
1158 virtio_cread_le_feature(vdev
, VIRTIO_IOMMU_F_INPUT_RANGE
,
1159 struct virtio_iommu_config
, input_range
.start
,
1162 virtio_cread_le_feature(vdev
, VIRTIO_IOMMU_F_INPUT_RANGE
,
1163 struct virtio_iommu_config
, input_range
.end
,
1166 virtio_cread_le_feature(vdev
, VIRTIO_IOMMU_F_DOMAIN_RANGE
,
1167 struct virtio_iommu_config
, domain_range
.start
,
1168 &viommu
->first_domain
);
1170 virtio_cread_le_feature(vdev
, VIRTIO_IOMMU_F_DOMAIN_RANGE
,
1171 struct virtio_iommu_config
, domain_range
.end
,
1172 &viommu
->last_domain
);
1174 virtio_cread_le_feature(vdev
, VIRTIO_IOMMU_F_PROBE
,
1175 struct virtio_iommu_config
, probe_size
,
1176 &viommu
->probe_size
);
1178 viommu
->geometry
= (struct iommu_domain_geometry
) {
1179 .aperture_start
= input_start
,
1180 .aperture_end
= input_end
,
1181 .force_aperture
= true,
1184 if (virtio_has_feature(vdev
, VIRTIO_IOMMU_F_MMIO
))
1185 viommu
->map_flags
|= VIRTIO_IOMMU_MAP_F_MMIO
;
1187 viommu_ops
.pgsize_bitmap
= viommu
->pgsize_bitmap
;
1189 virtio_device_ready(vdev
);
1191 /* Populate the event queue with buffers */
1192 ret
= viommu_fill_evtq(viommu
);
1196 ret
= iommu_device_sysfs_add(&viommu
->iommu
, dev
, NULL
, "%s",
1197 virtio_bus_name(vdev
));
1201 iommu_device_register(&viommu
->iommu
, &viommu_ops
, parent_dev
);
1203 vdev
->priv
= viommu
;
1205 dev_info(dev
, "input address: %u bits\n",
1206 order_base_2(viommu
->geometry
.aperture_end
));
1207 dev_info(dev
, "page mask: %#llx\n", viommu
->pgsize_bitmap
);
1212 vdev
->config
->del_vqs(vdev
);
1217 static void viommu_remove(struct virtio_device
*vdev
)
1219 struct viommu_dev
*viommu
= vdev
->priv
;
1221 iommu_device_sysfs_remove(&viommu
->iommu
);
1222 iommu_device_unregister(&viommu
->iommu
);
1224 /* Stop all virtqueues */
1225 virtio_reset_device(vdev
);
1226 vdev
->config
->del_vqs(vdev
);
1228 dev_info(&vdev
->dev
, "device removed\n");
1231 static void viommu_config_changed(struct virtio_device
*vdev
)
1233 dev_warn(&vdev
->dev
, "config changed\n");
1236 static unsigned int features
[] = {
1237 VIRTIO_IOMMU_F_MAP_UNMAP
,
1238 VIRTIO_IOMMU_F_INPUT_RANGE
,
1239 VIRTIO_IOMMU_F_DOMAIN_RANGE
,
1240 VIRTIO_IOMMU_F_PROBE
,
1241 VIRTIO_IOMMU_F_MMIO
,
1242 VIRTIO_IOMMU_F_BYPASS_CONFIG
,
1245 static struct virtio_device_id id_table
[] = {
1246 { VIRTIO_ID_IOMMU
, VIRTIO_DEV_ANY_ID
},
1249 MODULE_DEVICE_TABLE(virtio
, id_table
);
1251 static struct virtio_driver virtio_iommu_drv
= {
1252 .driver
.name
= KBUILD_MODNAME
,
1253 .id_table
= id_table
,
1254 .feature_table
= features
,
1255 .feature_table_size
= ARRAY_SIZE(features
),
1256 .probe
= viommu_probe
,
1257 .remove
= viommu_remove
,
1258 .config_changed
= viommu_config_changed
,
1261 module_virtio_driver(virtio_iommu_drv
);
1263 MODULE_DESCRIPTION("Virtio IOMMU driver");
1264 MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@arm.com>");
1265 MODULE_LICENSE("GPL v2");