1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * MMU-based software IOTLB.
4  *
5  * Copyright (C) 2020-2021 Bytedance Inc. and/or its affiliates. All rights reserved.
6  *
7  * Author: Xie Yongji <xieyongji@bytedance.com>
8  *
9  */
10 
11 #ifndef _VDUSE_IOVA_DOMAIN_H
12 #define _VDUSE_IOVA_DOMAIN_H
13 
14 #include <linux/iova.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/vhost_iotlb.h>
17 
18 #define IOVA_START_PFN 1
19 
20 #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
21 
22 struct vduse_bounce_map {
23 	struct page *bounce_page;
24 	u64 orig_phys;
25 };
26 
27 struct vduse_iova_domain {
28 	struct iova_domain stream_iovad;
29 	struct iova_domain consistent_iovad;
30 	struct vduse_bounce_map *bounce_maps;
31 	size_t bounce_size;
32 	unsigned long iova_limit;
33 	int bounce_map;
34 	struct vhost_iotlb *iotlb;
35 	spinlock_t iotlb_lock;
36 	struct file *file;
37 };
38 
39 int vduse_domain_set_map(struct vduse_iova_domain *domain,
40 			 struct vhost_iotlb *iotlb);
41 
42 void vduse_domain_clear_map(struct vduse_iova_domain *domain,
43 			    struct vhost_iotlb *iotlb);
44 
45 dma_addr_t vduse_domain_map_page(struct vduse_iova_domain *domain,
46 				 struct page *page, unsigned long offset,
47 				 size_t size, enum dma_data_direction dir,
48 				 unsigned long attrs);
49 
50 void vduse_domain_unmap_page(struct vduse_iova_domain *domain,
51 			     dma_addr_t dma_addr, size_t size,
52 			     enum dma_data_direction dir, unsigned long attrs);
53 
54 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain,
55 				  size_t size, dma_addr_t *dma_addr,
56 				  gfp_t flag, unsigned long attrs);
57 
58 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t size,
59 				void *vaddr, dma_addr_t dma_addr,
60 				unsigned long attrs);
61 
62 void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain);
63 
64 void vduse_domain_destroy(struct vduse_iova_domain *domain);
65 
66 struct vduse_iova_domain *vduse_domain_create(unsigned long iova_limit,
67 					      size_t bounce_size);
68 
69 int vduse_domain_init(void);
70 
71 void vduse_domain_exit(void);
72 
73 #endif /* _VDUSE_IOVA_DOMAIN_H */
74