1 // SPDX-License-Identifier: GPL-2.0
3 * Virtio driver for the paravirtualized IOMMU
5 * Copyright (C) 2018 Arm Limited
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/amba/bus.h>
11 #include <linux/delay.h>
12 #include <linux/dma-iommu.h>
13 #include <linux/freezer.h>
14 #include <linux/interval_tree.h>
15 #include <linux/iommu.h>
16 #include <linux/module.h>
17 #include <linux/of_iommu.h>
18 #include <linux/of_platform.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/virtio.h>
22 #include <linux/virtio_config.h>
23 #include <linux/virtio_ids.h>
24 #include <linux/wait.h>
26 #include <uapi/linux/virtio_iommu.h>
28 #define MSI_IOVA_BASE 0x8000000
29 #define MSI_IOVA_LENGTH 0x100000
31 #define VIOMMU_REQUEST_VQ 0
32 #define VIOMMU_EVENT_VQ 1
33 #define VIOMMU_NR_VQS 2
36 struct iommu_device iommu;
38 struct virtio_device *vdev;
40 struct ida domain_ids;
42 struct virtqueue *vqs[VIOMMU_NR_VQS];
43 spinlock_t request_lock;
44 struct list_head requests;
47 /* Device configuration */
48 struct iommu_domain_geometry geometry;
54 struct viommu_mapping {
56 struct interval_tree_node iova;
60 struct viommu_domain {
61 struct iommu_domain domain;
62 struct viommu_dev *viommu;
63 struct mutex mutex; /* protects viommu pointer */
66 spinlock_t mappings_lock;
67 struct rb_root_cached mappings;
69 unsigned long nr_endpoints;
72 struct viommu_endpoint {
74 struct viommu_dev *viommu;
75 struct viommu_domain *vdomain;
76 struct list_head resv_regions;
79 struct viommu_request {
80 struct list_head list;
82 unsigned int write_offset;
87 #define VIOMMU_FAULT_RESV_MASK 0xffffff00
92 struct virtio_iommu_fault fault;
96 #define to_viommu_domain(domain) \
97 container_of(domain, struct viommu_domain, domain)
99 static int viommu_get_req_errno(void *buf, size_t len)
101 struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
103 switch (tail->status) {
104 case VIRTIO_IOMMU_S_OK:
106 case VIRTIO_IOMMU_S_UNSUPP:
108 case VIRTIO_IOMMU_S_INVAL:
110 case VIRTIO_IOMMU_S_RANGE:
112 case VIRTIO_IOMMU_S_NOENT:
114 case VIRTIO_IOMMU_S_FAULT:
116 case VIRTIO_IOMMU_S_IOERR:
117 case VIRTIO_IOMMU_S_DEVERR:
123 static void viommu_set_req_status(void *buf, size_t len, int status)
125 struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
127 tail->status = status;
130 static off_t viommu_get_write_desc_offset(struct viommu_dev *viommu,
131 struct virtio_iommu_req_head *req,
134 size_t tail_size = sizeof(struct virtio_iommu_req_tail);
136 if (req->type == VIRTIO_IOMMU_T_PROBE)
137 return len - viommu->probe_size - tail_size;
139 return len - tail_size;
143 * __viommu_sync_req - Complete all in-flight requests
145 * Wait for all added requests to complete. When this function returns, all
146 * requests that were in-flight at the time of the call have completed.
148 static int __viommu_sync_req(struct viommu_dev *viommu)
153 struct viommu_request *req;
154 struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
156 assert_spin_locked(&viommu->request_lock);
160 while (!list_empty(&viommu->requests)) {
162 req = virtqueue_get_buf(vq, &len);
167 viommu_set_req_status(req->buf, req->len,
168 VIRTIO_IOMMU_S_IOERR);
170 write_len = req->len - req->write_offset;
171 if (req->writeback && len == write_len)
172 memcpy(req->writeback, req->buf + req->write_offset,
175 list_del(&req->list);
182 static int viommu_sync_req(struct viommu_dev *viommu)
187 spin_lock_irqsave(&viommu->request_lock, flags);
188 ret = __viommu_sync_req(viommu);
190 dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
191 spin_unlock_irqrestore(&viommu->request_lock, flags);
197 * __viommu_add_request - Add one request to the queue
198 * @buf: pointer to the request buffer
199 * @len: length of the request buffer
200 * @writeback: copy data back to the buffer when the request completes.
202 * Add a request to the queue. Only synchronize the queue if it's already full.
203 * Otherwise don't kick the queue nor wait for requests to complete.
205 * When @writeback is true, data written by the device, including the request
206 * status, is copied into @buf after the request completes. This is unsafe if
207 * the caller allocates @buf on stack and drops the lock between add_req() and
210 * Return 0 if the request was successfully added to the queue.
212 static int __viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len,
217 struct viommu_request *req;
218 struct scatterlist top_sg, bottom_sg;
219 struct scatterlist *sg[2] = { &top_sg, &bottom_sg };
220 struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
222 assert_spin_locked(&viommu->request_lock);
224 write_offset = viommu_get_write_desc_offset(viommu, buf, len);
225 if (write_offset <= 0)
228 req = kzalloc(sizeof(*req) + len, GFP_ATOMIC);
234 req->writeback = buf + write_offset;
235 req->write_offset = write_offset;
237 memcpy(&req->buf, buf, write_offset);
239 sg_init_one(&top_sg, req->buf, write_offset);
240 sg_init_one(&bottom_sg, req->buf + write_offset, len - write_offset);
242 ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
243 if (ret == -ENOSPC) {
244 /* If the queue is full, sync and retry */
245 if (!__viommu_sync_req(viommu))
246 ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
251 list_add_tail(&req->list, &viommu->requests);
259 static int viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len)
264 spin_lock_irqsave(&viommu->request_lock, flags);
265 ret = __viommu_add_req(viommu, buf, len, false);
267 dev_dbg(viommu->dev, "could not add request: %d\n", ret);
268 spin_unlock_irqrestore(&viommu->request_lock, flags);
274 * Send a request and wait for it to complete. Return the request status (as an
277 static int viommu_send_req_sync(struct viommu_dev *viommu, void *buf,
283 spin_lock_irqsave(&viommu->request_lock, flags);
285 ret = __viommu_add_req(viommu, buf, len, true);
287 dev_dbg(viommu->dev, "could not add request (%d)\n", ret);
291 ret = __viommu_sync_req(viommu);
293 dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
294 /* Fall-through (get the actual request status) */
297 ret = viommu_get_req_errno(buf, len);
299 spin_unlock_irqrestore(&viommu->request_lock, flags);
304 * viommu_add_mapping - add a mapping to the internal tree
306 * On success, return the new mapping. Otherwise return NULL.
308 static int viommu_add_mapping(struct viommu_domain *vdomain, unsigned long iova,
309 phys_addr_t paddr, size_t size, u32 flags)
311 unsigned long irqflags;
312 struct viommu_mapping *mapping;
314 mapping = kzalloc(sizeof(*mapping), GFP_ATOMIC);
318 mapping->paddr = paddr;
319 mapping->iova.start = iova;
320 mapping->iova.last = iova + size - 1;
321 mapping->flags = flags;
323 spin_lock_irqsave(&vdomain->mappings_lock, irqflags);
324 interval_tree_insert(&mapping->iova, &vdomain->mappings);
325 spin_unlock_irqrestore(&vdomain->mappings_lock, irqflags);
331 * viommu_del_mappings - remove mappings from the internal tree
333 * @vdomain: the domain
334 * @iova: start of the range
335 * @size: size of the range. A size of 0 corresponds to the entire address
338 * On success, returns the number of unmapped bytes (>= size)
340 static size_t viommu_del_mappings(struct viommu_domain *vdomain,
341 unsigned long iova, size_t size)
345 unsigned long last = iova + size - 1;
346 struct viommu_mapping *mapping = NULL;
347 struct interval_tree_node *node, *next;
349 spin_lock_irqsave(&vdomain->mappings_lock, flags);
350 next = interval_tree_iter_first(&vdomain->mappings, iova, last);
353 mapping = container_of(node, struct viommu_mapping, iova);
354 next = interval_tree_iter_next(node, iova, last);
356 /* Trying to split a mapping? */
357 if (mapping->iova.start < iova)
361 * Virtio-iommu doesn't allow UNMAP to split a mapping created
362 * with a single MAP request, so remove the full mapping.
364 unmapped += mapping->iova.last - mapping->iova.start + 1;
366 interval_tree_remove(node, &vdomain->mappings);
369 spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
375 * viommu_replay_mappings - re-send MAP requests
377 * When reattaching a domain that was previously detached from all endpoints,
378 * mappings were deleted from the device. Re-create the mappings available in
381 static int viommu_replay_mappings(struct viommu_domain *vdomain)
385 struct viommu_mapping *mapping;
386 struct interval_tree_node *node;
387 struct virtio_iommu_req_map map;
389 spin_lock_irqsave(&vdomain->mappings_lock, flags);
390 node = interval_tree_iter_first(&vdomain->mappings, 0, -1UL);
392 mapping = container_of(node, struct viommu_mapping, iova);
393 map = (struct virtio_iommu_req_map) {
394 .head.type = VIRTIO_IOMMU_T_MAP,
395 .domain = cpu_to_le32(vdomain->id),
396 .virt_start = cpu_to_le64(mapping->iova.start),
397 .virt_end = cpu_to_le64(mapping->iova.last),
398 .phys_start = cpu_to_le64(mapping->paddr),
399 .flags = cpu_to_le32(mapping->flags),
402 ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
406 node = interval_tree_iter_next(node, 0, -1UL);
408 spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
413 static int viommu_add_resv_mem(struct viommu_endpoint *vdev,
414 struct virtio_iommu_probe_resv_mem *mem,
419 phys_addr_t start, end;
420 struct iommu_resv_region *region = NULL;
421 unsigned long prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
423 start = start64 = le64_to_cpu(mem->start);
424 end = end64 = le64_to_cpu(mem->end);
425 size = end64 - start64 + 1;
427 /* Catch any overflow, including the unlikely end64 - start64 + 1 = 0 */
428 if (start != start64 || end != end64 || size < end64 - start64)
431 if (len < sizeof(*mem))
434 switch (mem->subtype) {
436 dev_warn(vdev->dev, "unknown resv mem subtype 0x%x\n",
439 case VIRTIO_IOMMU_RESV_MEM_T_RESERVED:
440 region = iommu_alloc_resv_region(start, size, 0,
441 IOMMU_RESV_RESERVED);
443 case VIRTIO_IOMMU_RESV_MEM_T_MSI:
444 region = iommu_alloc_resv_region(start, size, prot,
451 list_add(&vdev->resv_regions, ®ion->list);
455 static int viommu_probe_endpoint(struct viommu_dev *viommu, struct device *dev)
461 struct virtio_iommu_req_probe *probe;
462 struct virtio_iommu_probe_property *prop;
463 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
464 struct viommu_endpoint *vdev = fwspec->iommu_priv;
466 if (!fwspec->num_ids)
469 probe_len = sizeof(*probe) + viommu->probe_size +
470 sizeof(struct virtio_iommu_req_tail);
471 probe = kzalloc(probe_len, GFP_KERNEL);
475 probe->head.type = VIRTIO_IOMMU_T_PROBE;
477 * For now, assume that properties of an endpoint that outputs multiple
478 * IDs are consistent. Only probe the first one.
480 probe->endpoint = cpu_to_le32(fwspec->ids[0]);
482 ret = viommu_send_req_sync(viommu, probe, probe_len);
486 prop = (void *)probe->properties;
487 type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
489 while (type != VIRTIO_IOMMU_PROBE_T_NONE &&
490 cur < viommu->probe_size) {
491 len = le16_to_cpu(prop->length) + sizeof(*prop);
494 case VIRTIO_IOMMU_PROBE_T_RESV_MEM:
495 ret = viommu_add_resv_mem(vdev, (void *)prop, len);
498 dev_err(dev, "unknown viommu prop 0x%x\n", type);
502 dev_err(dev, "failed to parse viommu prop 0x%x\n", type);
505 if (cur >= viommu->probe_size)
508 prop = (void *)probe->properties + cur;
509 type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
517 static int viommu_fault_handler(struct viommu_dev *viommu,
518 struct virtio_iommu_fault *fault)
522 u8 reason = fault->reason;
523 u32 flags = le32_to_cpu(fault->flags);
524 u32 endpoint = le32_to_cpu(fault->endpoint);
525 u64 address = le64_to_cpu(fault->address);
528 case VIRTIO_IOMMU_FAULT_R_DOMAIN:
529 reason_str = "domain";
531 case VIRTIO_IOMMU_FAULT_R_MAPPING:
534 case VIRTIO_IOMMU_FAULT_R_UNKNOWN:
536 reason_str = "unknown";
540 /* TODO: find EP by ID and report_iommu_fault */
541 if (flags & VIRTIO_IOMMU_FAULT_F_ADDRESS)
542 dev_err_ratelimited(viommu->dev, "%s fault from EP %u at %#llx [%s%s%s]\n",
543 reason_str, endpoint, address,
544 flags & VIRTIO_IOMMU_FAULT_F_READ ? "R" : "",
545 flags & VIRTIO_IOMMU_FAULT_F_WRITE ? "W" : "",
546 flags & VIRTIO_IOMMU_FAULT_F_EXEC ? "X" : "");
548 dev_err_ratelimited(viommu->dev, "%s fault from EP %u\n",
549 reason_str, endpoint);
553 static void viommu_event_handler(struct virtqueue *vq)
557 struct scatterlist sg[1];
558 struct viommu_event *evt;
559 struct viommu_dev *viommu = vq->vdev->priv;
561 while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
562 if (len > sizeof(*evt)) {
564 "invalid event buffer (len %u != %zu)\n",
566 } else if (!(evt->head & VIOMMU_FAULT_RESV_MASK)) {
567 viommu_fault_handler(viommu, &evt->fault);
570 sg_init_one(sg, evt, sizeof(*evt));
571 ret = virtqueue_add_inbuf(vq, sg, 1, evt, GFP_ATOMIC);
573 dev_err(viommu->dev, "could not add event buffer\n");
581 static struct iommu_domain *viommu_domain_alloc(unsigned type)
583 struct viommu_domain *vdomain;
585 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
588 vdomain = kzalloc(sizeof(*vdomain), GFP_KERNEL);
592 mutex_init(&vdomain->mutex);
593 spin_lock_init(&vdomain->mappings_lock);
594 vdomain->mappings = RB_ROOT_CACHED;
596 if (type == IOMMU_DOMAIN_DMA &&
597 iommu_get_dma_cookie(&vdomain->domain)) {
602 return &vdomain->domain;
605 static int viommu_domain_finalise(struct viommu_dev *viommu,
606 struct iommu_domain *domain)
609 struct viommu_domain *vdomain = to_viommu_domain(domain);
610 unsigned int max_domain = viommu->domain_bits > 31 ? ~0 :
611 (1U << viommu->domain_bits) - 1;
613 vdomain->viommu = viommu;
615 domain->pgsize_bitmap = viommu->pgsize_bitmap;
616 domain->geometry = viommu->geometry;
618 ret = ida_alloc_max(&viommu->domain_ids, max_domain, GFP_KERNEL);
620 vdomain->id = (unsigned int)ret;
622 return ret > 0 ? 0 : ret;
625 static void viommu_domain_free(struct iommu_domain *domain)
627 struct viommu_domain *vdomain = to_viommu_domain(domain);
629 iommu_put_dma_cookie(domain);
631 /* Free all remaining mappings (size 2^64) */
632 viommu_del_mappings(vdomain, 0, 0);
635 ida_free(&vdomain->viommu->domain_ids, vdomain->id);
640 static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
644 struct virtio_iommu_req_attach req;
645 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
646 struct viommu_endpoint *vdev = fwspec->iommu_priv;
647 struct viommu_domain *vdomain = to_viommu_domain(domain);
649 mutex_lock(&vdomain->mutex);
650 if (!vdomain->viommu) {
652 * Properly initialize the domain now that we know which viommu
655 ret = viommu_domain_finalise(vdev->viommu, domain);
656 } else if (vdomain->viommu != vdev->viommu) {
657 dev_err(dev, "cannot attach to foreign vIOMMU\n");
660 mutex_unlock(&vdomain->mutex);
666 * In the virtio-iommu device, when attaching the endpoint to a new
667 * domain, it is detached from the old one and, if as as a result the
668 * old domain isn't attached to any endpoint, all mappings are removed
669 * from the old domain and it is freed.
671 * In the driver the old domain still exists, and its mappings will be
672 * recreated if it gets reattached to an endpoint. Otherwise it will be
675 * vdev->vdomain is protected by group->mutex
678 vdev->vdomain->nr_endpoints--;
680 req = (struct virtio_iommu_req_attach) {
681 .head.type = VIRTIO_IOMMU_T_ATTACH,
682 .domain = cpu_to_le32(vdomain->id),
685 for (i = 0; i < fwspec->num_ids; i++) {
686 req.endpoint = cpu_to_le32(fwspec->ids[i]);
688 ret = viommu_send_req_sync(vdomain->viommu, &req, sizeof(req));
693 if (!vdomain->nr_endpoints) {
695 * This endpoint is the first to be attached to the domain.
696 * Replay existing mappings (e.g. SW MSI).
698 ret = viommu_replay_mappings(vdomain);
703 vdomain->nr_endpoints++;
704 vdev->vdomain = vdomain;
709 static int viommu_map(struct iommu_domain *domain, unsigned long iova,
710 phys_addr_t paddr, size_t size, int prot)
714 struct virtio_iommu_req_map map;
715 struct viommu_domain *vdomain = to_viommu_domain(domain);
717 flags = (prot & IOMMU_READ ? VIRTIO_IOMMU_MAP_F_READ : 0) |
718 (prot & IOMMU_WRITE ? VIRTIO_IOMMU_MAP_F_WRITE : 0) |
719 (prot & IOMMU_MMIO ? VIRTIO_IOMMU_MAP_F_MMIO : 0);
721 ret = viommu_add_mapping(vdomain, iova, paddr, size, flags);
725 map = (struct virtio_iommu_req_map) {
726 .head.type = VIRTIO_IOMMU_T_MAP,
727 .domain = cpu_to_le32(vdomain->id),
728 .virt_start = cpu_to_le64(iova),
729 .phys_start = cpu_to_le64(paddr),
730 .virt_end = cpu_to_le64(iova + size - 1),
731 .flags = cpu_to_le32(flags),
734 if (!vdomain->nr_endpoints)
737 ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
739 viommu_del_mappings(vdomain, iova, size);
744 static size_t viommu_unmap(struct iommu_domain *domain, unsigned long iova,
749 struct virtio_iommu_req_unmap unmap;
750 struct viommu_domain *vdomain = to_viommu_domain(domain);
752 unmapped = viommu_del_mappings(vdomain, iova, size);
756 /* Device already removed all mappings after detach. */
757 if (!vdomain->nr_endpoints)
760 unmap = (struct virtio_iommu_req_unmap) {
761 .head.type = VIRTIO_IOMMU_T_UNMAP,
762 .domain = cpu_to_le32(vdomain->id),
763 .virt_start = cpu_to_le64(iova),
764 .virt_end = cpu_to_le64(iova + unmapped - 1),
767 ret = viommu_add_req(vdomain->viommu, &unmap, sizeof(unmap));
768 return ret ? 0 : unmapped;
771 static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
776 struct viommu_mapping *mapping;
777 struct interval_tree_node *node;
778 struct viommu_domain *vdomain = to_viommu_domain(domain);
780 spin_lock_irqsave(&vdomain->mappings_lock, flags);
781 node = interval_tree_iter_first(&vdomain->mappings, iova, iova);
783 mapping = container_of(node, struct viommu_mapping, iova);
784 paddr = mapping->paddr + (iova - mapping->iova.start);
786 spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
791 static void viommu_iotlb_sync(struct iommu_domain *domain)
793 struct viommu_domain *vdomain = to_viommu_domain(domain);
795 viommu_sync_req(vdomain->viommu);
798 static void viommu_get_resv_regions(struct device *dev, struct list_head *head)
800 struct iommu_resv_region *entry, *new_entry, *msi = NULL;
801 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
802 struct viommu_endpoint *vdev = fwspec->iommu_priv;
803 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
805 list_for_each_entry(entry, &vdev->resv_regions, list) {
806 if (entry->type == IOMMU_RESV_MSI)
809 new_entry = kmemdup(entry, sizeof(*entry), GFP_KERNEL);
812 list_add_tail(&new_entry->list, head);
816 * If the device didn't register any bypass MSI window, add a
817 * software-mapped region.
820 msi = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
821 prot, IOMMU_RESV_SW_MSI);
825 list_add_tail(&msi->list, head);
828 iommu_dma_get_resv_regions(dev, head);
831 static void viommu_put_resv_regions(struct device *dev, struct list_head *head)
833 struct iommu_resv_region *entry, *next;
835 list_for_each_entry_safe(entry, next, head, list)
839 static struct iommu_ops viommu_ops;
840 static struct virtio_driver virtio_iommu_drv;
842 static int viommu_match_node(struct device *dev, const void *data)
844 return dev->parent->fwnode == data;
847 static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode)
849 struct device *dev = driver_find_device(&virtio_iommu_drv.driver, NULL,
850 fwnode, viommu_match_node);
853 return dev ? dev_to_virtio(dev)->priv : NULL;
856 static int viommu_add_device(struct device *dev)
859 struct iommu_group *group;
860 struct viommu_endpoint *vdev;
861 struct viommu_dev *viommu = NULL;
862 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
864 if (!fwspec || fwspec->ops != &viommu_ops)
867 viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode);
871 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
876 vdev->viommu = viommu;
877 INIT_LIST_HEAD(&vdev->resv_regions);
878 fwspec->iommu_priv = vdev;
880 if (viommu->probe_size) {
881 /* Get additional information for this endpoint */
882 ret = viommu_probe_endpoint(viommu, dev);
887 ret = iommu_device_link(&viommu->iommu, dev);
892 * Last step creates a default domain and attaches to it. Everything
895 group = iommu_group_get_for_dev(dev);
897 ret = PTR_ERR(group);
901 iommu_group_put(group);
903 return PTR_ERR_OR_ZERO(group);
906 iommu_device_unlink(&viommu->iommu, dev);
908 viommu_put_resv_regions(dev, &vdev->resv_regions);
914 static void viommu_remove_device(struct device *dev)
916 struct viommu_endpoint *vdev;
917 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
919 if (!fwspec || fwspec->ops != &viommu_ops)
922 vdev = fwspec->iommu_priv;
924 iommu_group_remove_device(dev);
925 iommu_device_unlink(&vdev->viommu->iommu, dev);
926 viommu_put_resv_regions(dev, &vdev->resv_regions);
930 static struct iommu_group *viommu_device_group(struct device *dev)
933 return pci_device_group(dev);
935 return generic_device_group(dev);
938 static int viommu_of_xlate(struct device *dev, struct of_phandle_args *args)
940 return iommu_fwspec_add_ids(dev, args->args, 1);
943 static struct iommu_ops viommu_ops = {
944 .domain_alloc = viommu_domain_alloc,
945 .domain_free = viommu_domain_free,
946 .attach_dev = viommu_attach_dev,
948 .unmap = viommu_unmap,
949 .iova_to_phys = viommu_iova_to_phys,
950 .iotlb_sync = viommu_iotlb_sync,
951 .add_device = viommu_add_device,
952 .remove_device = viommu_remove_device,
953 .device_group = viommu_device_group,
954 .get_resv_regions = viommu_get_resv_regions,
955 .put_resv_regions = viommu_put_resv_regions,
956 .of_xlate = viommu_of_xlate,
959 static int viommu_init_vqs(struct viommu_dev *viommu)
961 struct virtio_device *vdev = dev_to_virtio(viommu->dev);
962 const char *names[] = { "request", "event" };
963 vq_callback_t *callbacks[] = {
964 NULL, /* No async requests */
965 viommu_event_handler,
968 return virtio_find_vqs(vdev, VIOMMU_NR_VQS, viommu->vqs, callbacks,
972 static int viommu_fill_evtq(struct viommu_dev *viommu)
975 struct scatterlist sg[1];
976 struct viommu_event *evts;
977 struct virtqueue *vq = viommu->vqs[VIOMMU_EVENT_VQ];
978 size_t nr_evts = vq->num_free;
980 viommu->evts = evts = devm_kmalloc_array(viommu->dev, nr_evts,
981 sizeof(*evts), GFP_KERNEL);
985 for (i = 0; i < nr_evts; i++) {
986 sg_init_one(sg, &evts[i], sizeof(*evts));
987 ret = virtqueue_add_inbuf(vq, sg, 1, &evts[i], GFP_KERNEL);
995 static int viommu_probe(struct virtio_device *vdev)
997 struct device *parent_dev = vdev->dev.parent;
998 struct viommu_dev *viommu = NULL;
999 struct device *dev = &vdev->dev;
1000 u64 input_start = 0;
1001 u64 input_end = -1UL;
1004 if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
1005 !virtio_has_feature(vdev, VIRTIO_IOMMU_F_MAP_UNMAP))
1008 viommu = devm_kzalloc(dev, sizeof(*viommu), GFP_KERNEL);
1012 spin_lock_init(&viommu->request_lock);
1013 ida_init(&viommu->domain_ids);
1015 viommu->vdev = vdev;
1016 INIT_LIST_HEAD(&viommu->requests);
1018 ret = viommu_init_vqs(viommu);
1022 virtio_cread(vdev, struct virtio_iommu_config, page_size_mask,
1023 &viommu->pgsize_bitmap);
1025 if (!viommu->pgsize_bitmap) {
1030 viommu->domain_bits = 32;
1032 /* Optional features */
1033 virtio_cread_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
1034 struct virtio_iommu_config, input_range.start,
1037 virtio_cread_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
1038 struct virtio_iommu_config, input_range.end,
1041 virtio_cread_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_BITS,
1042 struct virtio_iommu_config, domain_bits,
1043 &viommu->domain_bits);
1045 virtio_cread_feature(vdev, VIRTIO_IOMMU_F_PROBE,
1046 struct virtio_iommu_config, probe_size,
1047 &viommu->probe_size);
1049 viommu->geometry = (struct iommu_domain_geometry) {
1050 .aperture_start = input_start,
1051 .aperture_end = input_end,
1052 .force_aperture = true,
1055 viommu_ops.pgsize_bitmap = viommu->pgsize_bitmap;
1057 virtio_device_ready(vdev);
1059 /* Populate the event queue with buffers */
1060 ret = viommu_fill_evtq(viommu);
1064 ret = iommu_device_sysfs_add(&viommu->iommu, dev, NULL, "%s",
1065 virtio_bus_name(vdev));
1069 iommu_device_set_ops(&viommu->iommu, &viommu_ops);
1070 iommu_device_set_fwnode(&viommu->iommu, parent_dev->fwnode);
1072 iommu_device_register(&viommu->iommu);
1075 if (pci_bus_type.iommu_ops != &viommu_ops) {
1077 ret = bus_set_iommu(&pci_bus_type, &viommu_ops);
1079 goto err_unregister;
1082 #ifdef CONFIG_ARM_AMBA
1083 if (amba_bustype.iommu_ops != &viommu_ops) {
1084 ret = bus_set_iommu(&amba_bustype, &viommu_ops);
1086 goto err_unregister;
1089 if (platform_bus_type.iommu_ops != &viommu_ops) {
1090 ret = bus_set_iommu(&platform_bus_type, &viommu_ops);
1092 goto err_unregister;
1095 vdev->priv = viommu;
1097 dev_info(dev, "input address: %u bits\n",
1098 order_base_2(viommu->geometry.aperture_end));
1099 dev_info(dev, "page mask: %#llx\n", viommu->pgsize_bitmap);
1104 iommu_device_sysfs_remove(&viommu->iommu);
1105 iommu_device_unregister(&viommu->iommu);
1107 vdev->config->del_vqs(vdev);
1112 static void viommu_remove(struct virtio_device *vdev)
1114 struct viommu_dev *viommu = vdev->priv;
1116 iommu_device_sysfs_remove(&viommu->iommu);
1117 iommu_device_unregister(&viommu->iommu);
1119 /* Stop all virtqueues */
1120 vdev->config->reset(vdev);
1121 vdev->config->del_vqs(vdev);
1123 dev_info(&vdev->dev, "device removed\n");
1126 static void viommu_config_changed(struct virtio_device *vdev)
1128 dev_warn(&vdev->dev, "config changed\n");
1131 static unsigned int features[] = {
1132 VIRTIO_IOMMU_F_MAP_UNMAP,
1133 VIRTIO_IOMMU_F_DOMAIN_BITS,
1134 VIRTIO_IOMMU_F_INPUT_RANGE,
1135 VIRTIO_IOMMU_F_PROBE,
1138 static struct virtio_device_id id_table[] = {
1139 { VIRTIO_ID_IOMMU, VIRTIO_DEV_ANY_ID },
1143 static struct virtio_driver virtio_iommu_drv = {
1144 .driver.name = KBUILD_MODNAME,
1145 .driver.owner = THIS_MODULE,
1146 .id_table = id_table,
1147 .feature_table = features,
1148 .feature_table_size = ARRAY_SIZE(features),
1149 .probe = viommu_probe,
1150 .remove = viommu_remove,
1151 .config_changed = viommu_config_changed,
1154 module_virtio_driver(virtio_iommu_drv);
1156 MODULE_DESCRIPTION("Virtio IOMMU driver");
1157 MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@arm.com>");
1158 MODULE_LICENSE("GPL v2");