]> git.ipfire.org Git - people/ms/linux.git/blame - drivers/iommu/virtio-iommu.c
Merge tag 'net-6.0-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[people/ms/linux.git] / drivers / iommu / virtio-iommu.c
CommitLineData
edcd69ab
JPB
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Virtio driver for the paravirtualized IOMMU
4 *
ae24fb49 5 * Copyright (C) 2019 Arm Limited
edcd69ab
JPB
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/amba/bus.h>
11#include <linux/delay.h>
12#include <linux/dma-iommu.h>
8ce4904b 13#include <linux/dma-map-ops.h>
edcd69ab
JPB
14#include <linux/freezer.h>
15#include <linux/interval_tree.h>
16#include <linux/iommu.h>
17#include <linux/module.h>
edcd69ab
JPB
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>
25
26#include <uapi/linux/virtio_iommu.h>
27
28#define MSI_IOVA_BASE 0x8000000
29#define MSI_IOVA_LENGTH 0x100000
30
31#define VIOMMU_REQUEST_VQ 0
169a126c
JPB
32#define VIOMMU_EVENT_VQ 1
33#define VIOMMU_NR_VQS 2
edcd69ab
JPB
34
35struct viommu_dev {
36 struct iommu_device iommu;
37 struct device *dev;
38 struct virtio_device *vdev;
39
40 struct ida domain_ids;
41
42 struct virtqueue *vqs[VIOMMU_NR_VQS];
43 spinlock_t request_lock;
44 struct list_head requests;
169a126c 45 void *evts;
edcd69ab
JPB
46
47 /* Device configuration */
48 struct iommu_domain_geometry geometry;
49 u64 pgsize_bitmap;
ae24fb49
JPB
50 u32 first_domain;
51 u32 last_domain;
52 /* Supported MAP flags */
53 u32 map_flags;
2a5a3148 54 u32 probe_size;
edcd69ab
JPB
55};
56
57struct viommu_mapping {
58 phys_addr_t paddr;
59 struct interval_tree_node iova;
60 u32 flags;
61};
62
63struct viommu_domain {
64 struct iommu_domain domain;
65 struct viommu_dev *viommu;
66 struct mutex mutex; /* protects viommu pointer */
67 unsigned int id;
ae24fb49 68 u32 map_flags;
edcd69ab
JPB
69
70 spinlock_t mappings_lock;
71 struct rb_root_cached mappings;
72
73 unsigned long nr_endpoints;
f0f07a84 74 bool bypass;
edcd69ab
JPB
75};
76
77struct viommu_endpoint {
2a5a3148 78 struct device *dev;
edcd69ab
JPB
79 struct viommu_dev *viommu;
80 struct viommu_domain *vdomain;
2a5a3148 81 struct list_head resv_regions;
edcd69ab
JPB
82};
83
84struct viommu_request {
85 struct list_head list;
86 void *writeback;
87 unsigned int write_offset;
88 unsigned int len;
89 char buf[];
90};
91
169a126c
JPB
92#define VIOMMU_FAULT_RESV_MASK 0xffffff00
93
94struct viommu_event {
95 union {
96 u32 head;
97 struct virtio_iommu_fault fault;
98 };
99};
100
edcd69ab
JPB
101#define to_viommu_domain(domain) \
102 container_of(domain, struct viommu_domain, domain)
103
104static int viommu_get_req_errno(void *buf, size_t len)
105{
106 struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
107
108 switch (tail->status) {
109 case VIRTIO_IOMMU_S_OK:
110 return 0;
111 case VIRTIO_IOMMU_S_UNSUPP:
112 return -ENOSYS;
113 case VIRTIO_IOMMU_S_INVAL:
114 return -EINVAL;
115 case VIRTIO_IOMMU_S_RANGE:
116 return -ERANGE;
117 case VIRTIO_IOMMU_S_NOENT:
118 return -ENOENT;
119 case VIRTIO_IOMMU_S_FAULT:
120 return -EFAULT;
ae24fb49
JPB
121 case VIRTIO_IOMMU_S_NOMEM:
122 return -ENOMEM;
edcd69ab
JPB
123 case VIRTIO_IOMMU_S_IOERR:
124 case VIRTIO_IOMMU_S_DEVERR:
125 default:
126 return -EIO;
127 }
128}
129
130static void viommu_set_req_status(void *buf, size_t len, int status)
131{
132 struct virtio_iommu_req_tail *tail = buf + len - sizeof(*tail);
133
134 tail->status = status;
135}
136
137static off_t viommu_get_write_desc_offset(struct viommu_dev *viommu,
138 struct virtio_iommu_req_head *req,
139 size_t len)
140{
141 size_t tail_size = sizeof(struct virtio_iommu_req_tail);
142
2a5a3148
JPB
143 if (req->type == VIRTIO_IOMMU_T_PROBE)
144 return len - viommu->probe_size - tail_size;
145
edcd69ab
JPB
146 return len - tail_size;
147}
148
149/*
150 * __viommu_sync_req - Complete all in-flight requests
151 *
152 * Wait for all added requests to complete. When this function returns, all
153 * requests that were in-flight at the time of the call have completed.
154 */
155static int __viommu_sync_req(struct viommu_dev *viommu)
156{
edcd69ab
JPB
157 unsigned int len;
158 size_t write_len;
159 struct viommu_request *req;
160 struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
161
162 assert_spin_locked(&viommu->request_lock);
163
164 virtqueue_kick(vq);
165
166 while (!list_empty(&viommu->requests)) {
167 len = 0;
168 req = virtqueue_get_buf(vq, &len);
169 if (!req)
170 continue;
171
172 if (!len)
173 viommu_set_req_status(req->buf, req->len,
174 VIRTIO_IOMMU_S_IOERR);
175
176 write_len = req->len - req->write_offset;
177 if (req->writeback && len == write_len)
178 memcpy(req->writeback, req->buf + req->write_offset,
179 write_len);
180
181 list_del(&req->list);
182 kfree(req);
183 }
184
c1c8058d 185 return 0;
edcd69ab
JPB
186}
187
188static int viommu_sync_req(struct viommu_dev *viommu)
189{
190 int ret;
191 unsigned long flags;
192
193 spin_lock_irqsave(&viommu->request_lock, flags);
194 ret = __viommu_sync_req(viommu);
195 if (ret)
196 dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
197 spin_unlock_irqrestore(&viommu->request_lock, flags);
198
199 return ret;
200}
201
202/*
203 * __viommu_add_request - Add one request to the queue
204 * @buf: pointer to the request buffer
205 * @len: length of the request buffer
206 * @writeback: copy data back to the buffer when the request completes.
207 *
208 * Add a request to the queue. Only synchronize the queue if it's already full.
209 * Otherwise don't kick the queue nor wait for requests to complete.
210 *
211 * When @writeback is true, data written by the device, including the request
212 * status, is copied into @buf after the request completes. This is unsafe if
213 * the caller allocates @buf on stack and drops the lock between add_req() and
214 * sync_req().
215 *
216 * Return 0 if the request was successfully added to the queue.
217 */
218static int __viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len,
219 bool writeback)
220{
221 int ret;
222 off_t write_offset;
223 struct viommu_request *req;
224 struct scatterlist top_sg, bottom_sg;
225 struct scatterlist *sg[2] = { &top_sg, &bottom_sg };
226 struct virtqueue *vq = viommu->vqs[VIOMMU_REQUEST_VQ];
227
228 assert_spin_locked(&viommu->request_lock);
229
230 write_offset = viommu_get_write_desc_offset(viommu, buf, len);
231 if (write_offset <= 0)
232 return -EINVAL;
233
234 req = kzalloc(sizeof(*req) + len, GFP_ATOMIC);
235 if (!req)
236 return -ENOMEM;
237
238 req->len = len;
239 if (writeback) {
240 req->writeback = buf + write_offset;
241 req->write_offset = write_offset;
242 }
243 memcpy(&req->buf, buf, write_offset);
244
245 sg_init_one(&top_sg, req->buf, write_offset);
246 sg_init_one(&bottom_sg, req->buf + write_offset, len - write_offset);
247
248 ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
249 if (ret == -ENOSPC) {
250 /* If the queue is full, sync and retry */
251 if (!__viommu_sync_req(viommu))
252 ret = virtqueue_add_sgs(vq, sg, 1, 1, req, GFP_ATOMIC);
253 }
254 if (ret)
255 goto err_free;
256
257 list_add_tail(&req->list, &viommu->requests);
258 return 0;
259
260err_free:
261 kfree(req);
262 return ret;
263}
264
265static int viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len)
266{
267 int ret;
268 unsigned long flags;
269
270 spin_lock_irqsave(&viommu->request_lock, flags);
271 ret = __viommu_add_req(viommu, buf, len, false);
272 if (ret)
273 dev_dbg(viommu->dev, "could not add request: %d\n", ret);
274 spin_unlock_irqrestore(&viommu->request_lock, flags);
275
276 return ret;
277}
278
279/*
280 * Send a request and wait for it to complete. Return the request status (as an
281 * errno)
282 */
283static int viommu_send_req_sync(struct viommu_dev *viommu, void *buf,
284 size_t len)
285{
286 int ret;
287 unsigned long flags;
288
289 spin_lock_irqsave(&viommu->request_lock, flags);
290
291 ret = __viommu_add_req(viommu, buf, len, true);
292 if (ret) {
293 dev_dbg(viommu->dev, "could not add request (%d)\n", ret);
294 goto out_unlock;
295 }
296
297 ret = __viommu_sync_req(viommu);
298 if (ret) {
299 dev_dbg(viommu->dev, "could not sync requests (%d)\n", ret);
300 /* Fall-through (get the actual request status) */
301 }
302
303 ret = viommu_get_req_errno(buf, len);
304out_unlock:
305 spin_unlock_irqrestore(&viommu->request_lock, flags);
306 return ret;
307}
308
309/*
310 * viommu_add_mapping - add a mapping to the internal tree
311 *
312 * On success, return the new mapping. Otherwise return NULL.
313 */
c0c76359
JPB
314static int viommu_add_mapping(struct viommu_domain *vdomain, u64 iova, u64 end,
315 phys_addr_t paddr, u32 flags)
edcd69ab
JPB
316{
317 unsigned long irqflags;
318 struct viommu_mapping *mapping;
319
320 mapping = kzalloc(sizeof(*mapping), GFP_ATOMIC);
321 if (!mapping)
322 return -ENOMEM;
323
324 mapping->paddr = paddr;
325 mapping->iova.start = iova;
c0c76359 326 mapping->iova.last = end;
edcd69ab
JPB
327 mapping->flags = flags;
328
329 spin_lock_irqsave(&vdomain->mappings_lock, irqflags);
330 interval_tree_insert(&mapping->iova, &vdomain->mappings);
331 spin_unlock_irqrestore(&vdomain->mappings_lock, irqflags);
332
333 return 0;
334}
335
336/*
337 * viommu_del_mappings - remove mappings from the internal tree
338 *
339 * @vdomain: the domain
340 * @iova: start of the range
c0c76359 341 * @end: end of the range
edcd69ab 342 *
c0c76359 343 * On success, returns the number of unmapped bytes
edcd69ab
JPB
344 */
345static size_t viommu_del_mappings(struct viommu_domain *vdomain,
c0c76359 346 u64 iova, u64 end)
edcd69ab
JPB
347{
348 size_t unmapped = 0;
349 unsigned long flags;
edcd69ab
JPB
350 struct viommu_mapping *mapping = NULL;
351 struct interval_tree_node *node, *next;
352
353 spin_lock_irqsave(&vdomain->mappings_lock, flags);
c0c76359 354 next = interval_tree_iter_first(&vdomain->mappings, iova, end);
edcd69ab
JPB
355 while (next) {
356 node = next;
357 mapping = container_of(node, struct viommu_mapping, iova);
c0c76359 358 next = interval_tree_iter_next(node, iova, end);
edcd69ab
JPB
359
360 /* Trying to split a mapping? */
361 if (mapping->iova.start < iova)
362 break;
363
364 /*
365 * Virtio-iommu doesn't allow UNMAP to split a mapping created
366 * with a single MAP request, so remove the full mapping.
367 */
368 unmapped += mapping->iova.last - mapping->iova.start + 1;
369
370 interval_tree_remove(node, &vdomain->mappings);
371 kfree(mapping);
372 }
373 spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
374
375 return unmapped;
376}
377
b03cbca4
JPB
378/*
379 * Fill the domain with identity mappings, skipping the device's reserved
380 * regions.
381 */
382static int viommu_domain_map_identity(struct viommu_endpoint *vdev,
383 struct viommu_domain *vdomain)
384{
385 int ret;
386 struct iommu_resv_region *resv;
387 u64 iova = vdomain->domain.geometry.aperture_start;
388 u64 limit = vdomain->domain.geometry.aperture_end;
389 u32 flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
390 unsigned long granule = 1UL << __ffs(vdomain->domain.pgsize_bitmap);
391
392 iova = ALIGN(iova, granule);
393 limit = ALIGN_DOWN(limit + 1, granule) - 1;
394
395 list_for_each_entry(resv, &vdev->resv_regions, list) {
396 u64 resv_start = ALIGN_DOWN(resv->start, granule);
397 u64 resv_end = ALIGN(resv->start + resv->length, granule) - 1;
398
399 if (resv_end < iova || resv_start > limit)
400 /* No overlap */
401 continue;
402
403 if (resv_start > iova) {
404 ret = viommu_add_mapping(vdomain, iova, resv_start - 1,
405 (phys_addr_t)iova, flags);
406 if (ret)
407 goto err_unmap;
408 }
409
410 if (resv_end >= limit)
411 return 0;
412
413 iova = resv_end + 1;
414 }
415
416 ret = viommu_add_mapping(vdomain, iova, limit, (phys_addr_t)iova,
417 flags);
418 if (ret)
419 goto err_unmap;
420 return 0;
421
422err_unmap:
423 viommu_del_mappings(vdomain, 0, iova);
424 return ret;
425}
426
edcd69ab
JPB
427/*
428 * viommu_replay_mappings - re-send MAP requests
429 *
430 * When reattaching a domain that was previously detached from all endpoints,
431 * mappings were deleted from the device. Re-create the mappings available in
432 * the internal tree.
433 */
434static int viommu_replay_mappings(struct viommu_domain *vdomain)
435{
436 int ret = 0;
437 unsigned long flags;
438 struct viommu_mapping *mapping;
439 struct interval_tree_node *node;
440 struct virtio_iommu_req_map map;
441
442 spin_lock_irqsave(&vdomain->mappings_lock, flags);
443 node = interval_tree_iter_first(&vdomain->mappings, 0, -1UL);
444 while (node) {
445 mapping = container_of(node, struct viommu_mapping, iova);
446 map = (struct virtio_iommu_req_map) {
447 .head.type = VIRTIO_IOMMU_T_MAP,
448 .domain = cpu_to_le32(vdomain->id),
449 .virt_start = cpu_to_le64(mapping->iova.start),
450 .virt_end = cpu_to_le64(mapping->iova.last),
451 .phys_start = cpu_to_le64(mapping->paddr),
452 .flags = cpu_to_le32(mapping->flags),
453 };
454
455 ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
456 if (ret)
457 break;
458
459 node = interval_tree_iter_next(node, 0, -1UL);
460 }
461 spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
462
463 return ret;
464}
465
2a5a3148
JPB
466static int viommu_add_resv_mem(struct viommu_endpoint *vdev,
467 struct virtio_iommu_probe_resv_mem *mem,
468 size_t len)
469{
470 size_t size;
471 u64 start64, end64;
472 phys_addr_t start, end;
56109794 473 struct iommu_resv_region *region = NULL, *next;
2a5a3148
JPB
474 unsigned long prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
475
476 start = start64 = le64_to_cpu(mem->start);
477 end = end64 = le64_to_cpu(mem->end);
478 size = end64 - start64 + 1;
479
480 /* Catch any overflow, including the unlikely end64 - start64 + 1 = 0 */
481 if (start != start64 || end != end64 || size < end64 - start64)
482 return -EOVERFLOW;
483
484 if (len < sizeof(*mem))
485 return -EINVAL;
486
487 switch (mem->subtype) {
488 default:
489 dev_warn(vdev->dev, "unknown resv mem subtype 0x%x\n",
490 mem->subtype);
df561f66 491 fallthrough;
2a5a3148
JPB
492 case VIRTIO_IOMMU_RESV_MEM_T_RESERVED:
493 region = iommu_alloc_resv_region(start, size, 0,
494 IOMMU_RESV_RESERVED);
495 break;
496 case VIRTIO_IOMMU_RESV_MEM_T_MSI:
497 region = iommu_alloc_resv_region(start, size, prot,
498 IOMMU_RESV_MSI);
499 break;
500 }
501 if (!region)
502 return -ENOMEM;
503
56109794
JPB
504 /* Keep the list sorted */
505 list_for_each_entry(next, &vdev->resv_regions, list) {
506 if (next->start > region->start)
507 break;
508 }
509 list_add_tail(&region->list, &next->list);
2a5a3148
JPB
510 return 0;
511}
512
513static int viommu_probe_endpoint(struct viommu_dev *viommu, struct device *dev)
514{
515 int ret;
516 u16 type, len;
517 size_t cur = 0;
518 size_t probe_len;
519 struct virtio_iommu_req_probe *probe;
520 struct virtio_iommu_probe_property *prop;
521 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
a4b6c2af 522 struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
2a5a3148
JPB
523
524 if (!fwspec->num_ids)
525 return -EINVAL;
526
527 probe_len = sizeof(*probe) + viommu->probe_size +
528 sizeof(struct virtio_iommu_req_tail);
529 probe = kzalloc(probe_len, GFP_KERNEL);
530 if (!probe)
531 return -ENOMEM;
532
533 probe->head.type = VIRTIO_IOMMU_T_PROBE;
534 /*
535 * For now, assume that properties of an endpoint that outputs multiple
536 * IDs are consistent. Only probe the first one.
537 */
538 probe->endpoint = cpu_to_le32(fwspec->ids[0]);
539
540 ret = viommu_send_req_sync(viommu, probe, probe_len);
541 if (ret)
542 goto out_free;
543
544 prop = (void *)probe->properties;
545 type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
546
547 while (type != VIRTIO_IOMMU_PROBE_T_NONE &&
548 cur < viommu->probe_size) {
549 len = le16_to_cpu(prop->length) + sizeof(*prop);
550
551 switch (type) {
552 case VIRTIO_IOMMU_PROBE_T_RESV_MEM:
553 ret = viommu_add_resv_mem(vdev, (void *)prop, len);
554 break;
555 default:
556 dev_err(dev, "unknown viommu prop 0x%x\n", type);
557 }
558
559 if (ret)
560 dev_err(dev, "failed to parse viommu prop 0x%x\n", type);
561
562 cur += len;
563 if (cur >= viommu->probe_size)
564 break;
565
566 prop = (void *)probe->properties + cur;
567 type = le16_to_cpu(prop->type) & VIRTIO_IOMMU_PROBE_T_MASK;
568 }
569
570out_free:
571 kfree(probe);
572 return ret;
573}
574
169a126c
JPB
575static int viommu_fault_handler(struct viommu_dev *viommu,
576 struct virtio_iommu_fault *fault)
577{
578 char *reason_str;
579
580 u8 reason = fault->reason;
581 u32 flags = le32_to_cpu(fault->flags);
582 u32 endpoint = le32_to_cpu(fault->endpoint);
583 u64 address = le64_to_cpu(fault->address);
584
585 switch (reason) {
586 case VIRTIO_IOMMU_FAULT_R_DOMAIN:
587 reason_str = "domain";
588 break;
589 case VIRTIO_IOMMU_FAULT_R_MAPPING:
590 reason_str = "page";
591 break;
592 case VIRTIO_IOMMU_FAULT_R_UNKNOWN:
593 default:
594 reason_str = "unknown";
595 break;
596 }
597
598 /* TODO: find EP by ID and report_iommu_fault */
599 if (flags & VIRTIO_IOMMU_FAULT_F_ADDRESS)
600 dev_err_ratelimited(viommu->dev, "%s fault from EP %u at %#llx [%s%s%s]\n",
601 reason_str, endpoint, address,
602 flags & VIRTIO_IOMMU_FAULT_F_READ ? "R" : "",
603 flags & VIRTIO_IOMMU_FAULT_F_WRITE ? "W" : "",
604 flags & VIRTIO_IOMMU_FAULT_F_EXEC ? "X" : "");
605 else
606 dev_err_ratelimited(viommu->dev, "%s fault from EP %u\n",
607 reason_str, endpoint);
608 return 0;
609}
610
611static void viommu_event_handler(struct virtqueue *vq)
612{
613 int ret;
614 unsigned int len;
615 struct scatterlist sg[1];
616 struct viommu_event *evt;
617 struct viommu_dev *viommu = vq->vdev->priv;
618
619 while ((evt = virtqueue_get_buf(vq, &len)) != NULL) {
620 if (len > sizeof(*evt)) {
621 dev_err(viommu->dev,
622 "invalid event buffer (len %u != %zu)\n",
623 len, sizeof(*evt));
624 } else if (!(evt->head & VIOMMU_FAULT_RESV_MASK)) {
625 viommu_fault_handler(viommu, &evt->fault);
626 }
627
628 sg_init_one(sg, evt, sizeof(*evt));
629 ret = virtqueue_add_inbuf(vq, sg, 1, evt, GFP_ATOMIC);
630 if (ret)
631 dev_err(viommu->dev, "could not add event buffer\n");
632 }
633
634 virtqueue_kick(vq);
635}
636
edcd69ab
JPB
637/* IOMMU API */
638
639static struct iommu_domain *viommu_domain_alloc(unsigned type)
640{
641 struct viommu_domain *vdomain;
642
f0f07a84
JPB
643 if (type != IOMMU_DOMAIN_UNMANAGED &&
644 type != IOMMU_DOMAIN_DMA &&
645 type != IOMMU_DOMAIN_IDENTITY)
edcd69ab
JPB
646 return NULL;
647
648 vdomain = kzalloc(sizeof(*vdomain), GFP_KERNEL);
649 if (!vdomain)
650 return NULL;
651
652 mutex_init(&vdomain->mutex);
653 spin_lock_init(&vdomain->mappings_lock);
654 vdomain->mappings = RB_ROOT_CACHED;
655
edcd69ab
JPB
656 return &vdomain->domain;
657}
658
39b3b3c9 659static int viommu_domain_finalise(struct viommu_endpoint *vdev,
edcd69ab
JPB
660 struct iommu_domain *domain)
661{
662 int ret;
39b3b3c9
JPB
663 unsigned long viommu_page_size;
664 struct viommu_dev *viommu = vdev->viommu;
edcd69ab 665 struct viommu_domain *vdomain = to_viommu_domain(domain);
edcd69ab 666
39b3b3c9
JPB
667 viommu_page_size = 1UL << __ffs(viommu->pgsize_bitmap);
668 if (viommu_page_size > PAGE_SIZE) {
669 dev_err(vdev->dev,
670 "granule 0x%lx larger than system page size 0x%lx\n",
671 viommu_page_size, PAGE_SIZE);
672 return -EINVAL;
673 }
674
7062af3e
JPB
675 ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain,
676 viommu->last_domain, GFP_KERNEL);
677 if (ret < 0)
678 return ret;
679
680 vdomain->id = (unsigned int)ret;
edcd69ab
JPB
681
682 domain->pgsize_bitmap = viommu->pgsize_bitmap;
683 domain->geometry = viommu->geometry;
684
7062af3e
JPB
685 vdomain->map_flags = viommu->map_flags;
686 vdomain->viommu = viommu;
edcd69ab 687
f0f07a84 688 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
b03cbca4
JPB
689 if (virtio_has_feature(viommu->vdev,
690 VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
691 vdomain->bypass = true;
692 return 0;
693 }
694
695 ret = viommu_domain_map_identity(vdev, vdomain);
696 if (ret) {
f0f07a84
JPB
697 ida_free(&viommu->domain_ids, vdomain->id);
698 vdomain->viommu = NULL;
699 return -EOPNOTSUPP;
700 }
f0f07a84
JPB
701 }
702
7062af3e 703 return 0;
edcd69ab
JPB
704}
705
706static void viommu_domain_free(struct iommu_domain *domain)
707{
708 struct viommu_domain *vdomain = to_viommu_domain(domain);
edcd69ab 709
c0c76359
JPB
710 /* Free all remaining mappings */
711 viommu_del_mappings(vdomain, 0, ULLONG_MAX);
edcd69ab
JPB
712
713 if (vdomain->viommu)
714 ida_free(&vdomain->viommu->domain_ids, vdomain->id);
715
716 kfree(vdomain);
717}
718
719static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
720{
721 int i;
722 int ret = 0;
723 struct virtio_iommu_req_attach req;
724 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
a4b6c2af 725 struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
edcd69ab
JPB
726 struct viommu_domain *vdomain = to_viommu_domain(domain);
727
728 mutex_lock(&vdomain->mutex);
729 if (!vdomain->viommu) {
730 /*
731 * Properly initialize the domain now that we know which viommu
732 * owns it.
733 */
39b3b3c9 734 ret = viommu_domain_finalise(vdev, domain);
edcd69ab
JPB
735 } else if (vdomain->viommu != vdev->viommu) {
736 dev_err(dev, "cannot attach to foreign vIOMMU\n");
737 ret = -EXDEV;
738 }
739 mutex_unlock(&vdomain->mutex);
740
741 if (ret)
742 return ret;
743
744 /*
745 * In the virtio-iommu device, when attaching the endpoint to a new
4cb3600e 746 * domain, it is detached from the old one and, if as a result the
edcd69ab
JPB
747 * old domain isn't attached to any endpoint, all mappings are removed
748 * from the old domain and it is freed.
749 *
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
752 * freed explicitly.
753 *
754 * vdev->vdomain is protected by group->mutex
755 */
756 if (vdev->vdomain)
757 vdev->vdomain->nr_endpoints--;
758
759 req = (struct virtio_iommu_req_attach) {
760 .head.type = VIRTIO_IOMMU_T_ATTACH,
761 .domain = cpu_to_le32(vdomain->id),
762 };
763
f0f07a84
JPB
764 if (vdomain->bypass)
765 req.flags |= cpu_to_le32(VIRTIO_IOMMU_ATTACH_F_BYPASS);
766
edcd69ab
JPB
767 for (i = 0; i < fwspec->num_ids; i++) {
768 req.endpoint = cpu_to_le32(fwspec->ids[i]);
769
770 ret = viommu_send_req_sync(vdomain->viommu, &req, sizeof(req));
771 if (ret)
772 return ret;
773 }
774
775 if (!vdomain->nr_endpoints) {
776 /*
777 * This endpoint is the first to be attached to the domain.
778 * Replay existing mappings (e.g. SW MSI).
779 */
780 ret = viommu_replay_mappings(vdomain);
781 if (ret)
782 return ret;
783 }
784
785 vdomain->nr_endpoints++;
786 vdev->vdomain = vdomain;
787
788 return 0;
789}
790
7e62edd7
TZ
791static int viommu_map_pages(struct iommu_domain *domain, unsigned long iova,
792 phys_addr_t paddr, size_t pgsize, size_t pgcount,
793 int prot, gfp_t gfp, size_t *mapped)
edcd69ab
JPB
794{
795 int ret;
ae24fb49 796 u32 flags;
7e62edd7 797 size_t size = pgsize * pgcount;
c0c76359 798 u64 end = iova + size - 1;
edcd69ab
JPB
799 struct virtio_iommu_req_map map;
800 struct viommu_domain *vdomain = to_viommu_domain(domain);
801
802 flags = (prot & IOMMU_READ ? VIRTIO_IOMMU_MAP_F_READ : 0) |
803 (prot & IOMMU_WRITE ? VIRTIO_IOMMU_MAP_F_WRITE : 0) |
804 (prot & IOMMU_MMIO ? VIRTIO_IOMMU_MAP_F_MMIO : 0);
805
ae24fb49
JPB
806 if (flags & ~vdomain->map_flags)
807 return -EINVAL;
808
c0c76359 809 ret = viommu_add_mapping(vdomain, iova, end, paddr, flags);
edcd69ab
JPB
810 if (ret)
811 return ret;
812
813 map = (struct virtio_iommu_req_map) {
814 .head.type = VIRTIO_IOMMU_T_MAP,
815 .domain = cpu_to_le32(vdomain->id),
816 .virt_start = cpu_to_le64(iova),
817 .phys_start = cpu_to_le64(paddr),
c0c76359 818 .virt_end = cpu_to_le64(end),
edcd69ab
JPB
819 .flags = cpu_to_le32(flags),
820 };
821
822 if (!vdomain->nr_endpoints)
823 return 0;
824
825 ret = viommu_send_req_sync(vdomain->viommu, &map, sizeof(map));
826 if (ret)
c0c76359 827 viommu_del_mappings(vdomain, iova, end);
7e62edd7
TZ
828 else if (mapped)
829 *mapped = size;
edcd69ab
JPB
830
831 return ret;
832}
833
7e62edd7
TZ
834static size_t viommu_unmap_pages(struct iommu_domain *domain, unsigned long iova,
835 size_t pgsize, size_t pgcount,
836 struct iommu_iotlb_gather *gather)
edcd69ab
JPB
837{
838 int ret = 0;
839 size_t unmapped;
840 struct virtio_iommu_req_unmap unmap;
841 struct viommu_domain *vdomain = to_viommu_domain(domain);
7e62edd7 842 size_t size = pgsize * pgcount;
edcd69ab 843
c0c76359 844 unmapped = viommu_del_mappings(vdomain, iova, iova + size - 1);
edcd69ab
JPB
845 if (unmapped < size)
846 return 0;
847
848 /* Device already removed all mappings after detach. */
849 if (!vdomain->nr_endpoints)
850 return unmapped;
851
852 unmap = (struct virtio_iommu_req_unmap) {
853 .head.type = VIRTIO_IOMMU_T_UNMAP,
854 .domain = cpu_to_le32(vdomain->id),
855 .virt_start = cpu_to_le64(iova),
856 .virt_end = cpu_to_le64(iova + unmapped - 1),
857 };
858
859 ret = viommu_add_req(vdomain->viommu, &unmap, sizeof(unmap));
860 return ret ? 0 : unmapped;
861}
862
863static phys_addr_t viommu_iova_to_phys(struct iommu_domain *domain,
864 dma_addr_t iova)
865{
866 u64 paddr = 0;
867 unsigned long flags;
868 struct viommu_mapping *mapping;
869 struct interval_tree_node *node;
870 struct viommu_domain *vdomain = to_viommu_domain(domain);
871
872 spin_lock_irqsave(&vdomain->mappings_lock, flags);
873 node = interval_tree_iter_first(&vdomain->mappings, iova, iova);
874 if (node) {
875 mapping = container_of(node, struct viommu_mapping, iova);
876 paddr = mapping->paddr + (iova - mapping->iova.start);
877 }
878 spin_unlock_irqrestore(&vdomain->mappings_lock, flags);
879
880 return paddr;
881}
882
56f8af5e
WD
883static void viommu_iotlb_sync(struct iommu_domain *domain,
884 struct iommu_iotlb_gather *gather)
edcd69ab
JPB
885{
886 struct viommu_domain *vdomain = to_viommu_domain(domain);
887
888 viommu_sync_req(vdomain->viommu);
889}
890
891static void viommu_get_resv_regions(struct device *dev, struct list_head *head)
892{
2a5a3148 893 struct iommu_resv_region *entry, *new_entry, *msi = NULL;
a4b6c2af 894 struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
edcd69ab
JPB
895 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
896
2a5a3148
JPB
897 list_for_each_entry(entry, &vdev->resv_regions, list) {
898 if (entry->type == IOMMU_RESV_MSI)
899 msi = entry;
900
901 new_entry = kmemdup(entry, sizeof(*entry), GFP_KERNEL);
902 if (!new_entry)
903 return;
904 list_add_tail(&new_entry->list, head);
905 }
906
907 /*
908 * If the device didn't register any bypass MSI window, add a
909 * software-mapped region.
910 */
911 if (!msi) {
912 msi = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
913 prot, IOMMU_RESV_SW_MSI);
914 if (!msi)
915 return;
916
917 list_add_tail(&msi->list, head);
918 }
edcd69ab 919
edcd69ab
JPB
920 iommu_dma_get_resv_regions(dev, head);
921}
922
edcd69ab
JPB
923static struct iommu_ops viommu_ops;
924static struct virtio_driver virtio_iommu_drv;
925
3a1d5384 926static int viommu_match_node(struct device *dev, const void *data)
edcd69ab
JPB
927{
928 return dev->parent->fwnode == data;
929}
930
931static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode)
932{
933 struct device *dev = driver_find_device(&virtio_iommu_drv.driver, NULL,
934 fwnode, viommu_match_node);
935 put_device(dev);
936
937 return dev ? dev_to_virtio(dev)->priv : NULL;
938}
939
21acf659 940static struct iommu_device *viommu_probe_device(struct device *dev)
edcd69ab
JPB
941{
942 int ret;
edcd69ab
JPB
943 struct viommu_endpoint *vdev;
944 struct viommu_dev *viommu = NULL;
945 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
946
947 if (!fwspec || fwspec->ops != &viommu_ops)
21acf659 948 return ERR_PTR(-ENODEV);
edcd69ab
JPB
949
950 viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode);
951 if (!viommu)
21acf659 952 return ERR_PTR(-ENODEV);
edcd69ab
JPB
953
954 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
955 if (!vdev)
21acf659 956 return ERR_PTR(-ENOMEM);
edcd69ab 957
2a5a3148 958 vdev->dev = dev;
edcd69ab 959 vdev->viommu = viommu;
2a5a3148 960 INIT_LIST_HEAD(&vdev->resv_regions);
a4b6c2af 961 dev_iommu_priv_set(dev, vdev);
edcd69ab 962
2a5a3148
JPB
963 if (viommu->probe_size) {
964 /* Get additional information for this endpoint */
965 ret = viommu_probe_endpoint(viommu, dev);
966 if (ret)
967 goto err_free_dev;
968 }
969
21acf659 970 return &viommu->iommu;
edcd69ab 971
edcd69ab 972err_free_dev:
ae3ff39a 973 iommu_put_resv_regions(dev, &vdev->resv_regions);
edcd69ab
JPB
974 kfree(vdev);
975
21acf659 976 return ERR_PTR(ret);
edcd69ab
JPB
977}
978
8ce4904b
JPB
979static void viommu_probe_finalize(struct device *dev)
980{
981#ifndef CONFIG_ARCH_HAS_SETUP_DMA_OPS
982 /* First clear the DMA ops in case we're switching from a DMA domain */
983 set_dma_ops(dev, NULL);
984 iommu_setup_dma_ops(dev, 0, U64_MAX);
985#endif
986}
987
21acf659 988static void viommu_release_device(struct device *dev)
edcd69ab 989{
4d26ba67 990 struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
edcd69ab 991
ae3ff39a 992 iommu_put_resv_regions(dev, &vdev->resv_regions);
edcd69ab
JPB
993 kfree(vdev);
994}
995
996static struct iommu_group *viommu_device_group(struct device *dev)
997{
998 if (dev_is_pci(dev))
999 return pci_device_group(dev);
1000 else
1001 return generic_device_group(dev);
1002}
1003
1004static int viommu_of_xlate(struct device *dev, struct of_phandle_args *args)
1005{
1006 return iommu_fwspec_add_ids(dev, args->args, 1);
1007}
1008
91c98fe7
JPB
1009static bool viommu_capable(enum iommu_cap cap)
1010{
1011 switch (cap) {
1012 case IOMMU_CAP_CACHE_COHERENCY:
1013 return true;
1014 default:
1015 return false;
1016 }
1017}
1018
edcd69ab 1019static struct iommu_ops viommu_ops = {
91c98fe7 1020 .capable = viommu_capable,
edcd69ab 1021 .domain_alloc = viommu_domain_alloc,
21acf659 1022 .probe_device = viommu_probe_device,
8ce4904b 1023 .probe_finalize = viommu_probe_finalize,
21acf659 1024 .release_device = viommu_release_device,
edcd69ab
JPB
1025 .device_group = viommu_device_group,
1026 .get_resv_regions = viommu_get_resv_regions,
edcd69ab 1027 .of_xlate = viommu_of_xlate,
c0aec668 1028 .owner = THIS_MODULE,
9a630a4b
LB
1029 .default_domain_ops = &(const struct iommu_domain_ops) {
1030 .attach_dev = viommu_attach_dev,
7e62edd7
TZ
1031 .map_pages = viommu_map_pages,
1032 .unmap_pages = viommu_unmap_pages,
9a630a4b
LB
1033 .iova_to_phys = viommu_iova_to_phys,
1034 .iotlb_sync = viommu_iotlb_sync,
1035 .free = viommu_domain_free,
1036 }
edcd69ab
JPB
1037};
1038
1039static int viommu_init_vqs(struct viommu_dev *viommu)
1040{
1041 struct virtio_device *vdev = dev_to_virtio(viommu->dev);
169a126c
JPB
1042 const char *names[] = { "request", "event" };
1043 vq_callback_t *callbacks[] = {
1044 NULL, /* No async requests */
1045 viommu_event_handler,
1046 };
edcd69ab 1047
169a126c
JPB
1048 return virtio_find_vqs(vdev, VIOMMU_NR_VQS, viommu->vqs, callbacks,
1049 names, NULL);
1050}
edcd69ab 1051
169a126c
JPB
1052static int viommu_fill_evtq(struct viommu_dev *viommu)
1053{
1054 int i, ret;
1055 struct scatterlist sg[1];
1056 struct viommu_event *evts;
1057 struct virtqueue *vq = viommu->vqs[VIOMMU_EVENT_VQ];
1058 size_t nr_evts = vq->num_free;
1059
1060 viommu->evts = evts = devm_kmalloc_array(viommu->dev, nr_evts,
1061 sizeof(*evts), GFP_KERNEL);
1062 if (!evts)
1063 return -ENOMEM;
1064
1065 for (i = 0; i < nr_evts; i++) {
1066 sg_init_one(sg, &evts[i], sizeof(*evts));
1067 ret = virtqueue_add_inbuf(vq, sg, 1, &evts[i], GFP_KERNEL);
1068 if (ret)
1069 return ret;
1070 }
edcd69ab
JPB
1071
1072 return 0;
1073}
1074
1075static int viommu_probe(struct virtio_device *vdev)
1076{
1077 struct device *parent_dev = vdev->dev.parent;
1078 struct viommu_dev *viommu = NULL;
1079 struct device *dev = &vdev->dev;
1080 u64 input_start = 0;
1081 u64 input_end = -1UL;
1082 int ret;
1083
1084 if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
1085 !virtio_has_feature(vdev, VIRTIO_IOMMU_F_MAP_UNMAP))
1086 return -ENODEV;
1087
1088 viommu = devm_kzalloc(dev, sizeof(*viommu), GFP_KERNEL);
1089 if (!viommu)
1090 return -ENOMEM;
1091
1092 spin_lock_init(&viommu->request_lock);
1093 ida_init(&viommu->domain_ids);
1094 viommu->dev = dev;
1095 viommu->vdev = vdev;
1096 INIT_LIST_HEAD(&viommu->requests);
1097
1098 ret = viommu_init_vqs(viommu);
1099 if (ret)
1100 return ret;
1101
d83c67c4
MT
1102 virtio_cread_le(vdev, struct virtio_iommu_config, page_size_mask,
1103 &viommu->pgsize_bitmap);
edcd69ab
JPB
1104
1105 if (!viommu->pgsize_bitmap) {
1106 ret = -EINVAL;
1107 goto err_free_vqs;
1108 }
1109
ae24fb49
JPB
1110 viommu->map_flags = VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE;
1111 viommu->last_domain = ~0U;
edcd69ab
JPB
1112
1113 /* Optional features */
d83c67c4
MT
1114 virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
1115 struct virtio_iommu_config, input_range.start,
1116 &input_start);
edcd69ab 1117
d83c67c4
MT
1118 virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_INPUT_RANGE,
1119 struct virtio_iommu_config, input_range.end,
1120 &input_end);
edcd69ab 1121
d83c67c4
MT
1122 virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
1123 struct virtio_iommu_config, domain_range.start,
1124 &viommu->first_domain);
ae24fb49 1125
d83c67c4
MT
1126 virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_DOMAIN_RANGE,
1127 struct virtio_iommu_config, domain_range.end,
1128 &viommu->last_domain);
edcd69ab 1129
d83c67c4
MT
1130 virtio_cread_le_feature(vdev, VIRTIO_IOMMU_F_PROBE,
1131 struct virtio_iommu_config, probe_size,
1132 &viommu->probe_size);
2a5a3148 1133
edcd69ab
JPB
1134 viommu->geometry = (struct iommu_domain_geometry) {
1135 .aperture_start = input_start,
1136 .aperture_end = input_end,
1137 .force_aperture = true,
1138 };
1139
ae24fb49
JPB
1140 if (virtio_has_feature(vdev, VIRTIO_IOMMU_F_MMIO))
1141 viommu->map_flags |= VIRTIO_IOMMU_MAP_F_MMIO;
1142
edcd69ab
JPB
1143 viommu_ops.pgsize_bitmap = viommu->pgsize_bitmap;
1144
1145 virtio_device_ready(vdev);
1146
169a126c
JPB
1147 /* Populate the event queue with buffers */
1148 ret = viommu_fill_evtq(viommu);
1149 if (ret)
1150 goto err_free_vqs;
1151
edcd69ab
JPB
1152 ret = iommu_device_sysfs_add(&viommu->iommu, dev, NULL, "%s",
1153 virtio_bus_name(vdev));
1154 if (ret)
1155 goto err_free_vqs;
1156
2d471b20 1157 iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
edcd69ab
JPB
1158
1159#ifdef CONFIG_PCI
1160 if (pci_bus_type.iommu_ops != &viommu_ops) {
edcd69ab
JPB
1161 ret = bus_set_iommu(&pci_bus_type, &viommu_ops);
1162 if (ret)
1163 goto err_unregister;
1164 }
1165#endif
1166#ifdef CONFIG_ARM_AMBA
1167 if (amba_bustype.iommu_ops != &viommu_ops) {
1168 ret = bus_set_iommu(&amba_bustype, &viommu_ops);
1169 if (ret)
1170 goto err_unregister;
1171 }
1172#endif
1173 if (platform_bus_type.iommu_ops != &viommu_ops) {
1174 ret = bus_set_iommu(&platform_bus_type, &viommu_ops);
1175 if (ret)
1176 goto err_unregister;
1177 }
1178
1179 vdev->priv = viommu;
1180
1181 dev_info(dev, "input address: %u bits\n",
1182 order_base_2(viommu->geometry.aperture_end));
1183 dev_info(dev, "page mask: %#llx\n", viommu->pgsize_bitmap);
1184
1185 return 0;
1186
1187err_unregister:
1188 iommu_device_sysfs_remove(&viommu->iommu);
1189 iommu_device_unregister(&viommu->iommu);
1190err_free_vqs:
1191 vdev->config->del_vqs(vdev);
1192
1193 return ret;
1194}
1195
1196static void viommu_remove(struct virtio_device *vdev)
1197{
1198 struct viommu_dev *viommu = vdev->priv;
1199
1200 iommu_device_sysfs_remove(&viommu->iommu);
1201 iommu_device_unregister(&viommu->iommu);
1202
1203 /* Stop all virtqueues */
d9679d00 1204 virtio_reset_device(vdev);
edcd69ab
JPB
1205 vdev->config->del_vqs(vdev);
1206
1207 dev_info(&vdev->dev, "device removed\n");
1208}
1209
1210static void viommu_config_changed(struct virtio_device *vdev)
1211{
1212 dev_warn(&vdev->dev, "config changed\n");
1213}
1214
1215static unsigned int features[] = {
1216 VIRTIO_IOMMU_F_MAP_UNMAP,
edcd69ab 1217 VIRTIO_IOMMU_F_INPUT_RANGE,
ae24fb49 1218 VIRTIO_IOMMU_F_DOMAIN_RANGE,
2a5a3148 1219 VIRTIO_IOMMU_F_PROBE,
ae24fb49 1220 VIRTIO_IOMMU_F_MMIO,
f0f07a84 1221 VIRTIO_IOMMU_F_BYPASS_CONFIG,
edcd69ab
JPB
1222};
1223
1224static struct virtio_device_id id_table[] = {
1225 { VIRTIO_ID_IOMMU, VIRTIO_DEV_ANY_ID },
1226 { 0 },
1227};
382d91fc 1228MODULE_DEVICE_TABLE(virtio, id_table);
edcd69ab
JPB
1229
1230static struct virtio_driver virtio_iommu_drv = {
1231 .driver.name = KBUILD_MODNAME,
1232 .driver.owner = THIS_MODULE,
1233 .id_table = id_table,
1234 .feature_table = features,
1235 .feature_table_size = ARRAY_SIZE(features),
1236 .probe = viommu_probe,
1237 .remove = viommu_remove,
1238 .config_changed = viommu_config_changed,
1239};
1240
1241module_virtio_driver(virtio_iommu_drv);
1242
1243MODULE_DESCRIPTION("Virtio IOMMU driver");
1244MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@arm.com>");
1245MODULE_LICENSE("GPL v2");