xref: /linux/drivers/vdpa/vdpa_user/iova_domain.h (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
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 	struct page *user_bounce_page;
25 	u64 orig_phys;
26 };
27 
28 struct vduse_iova_domain {
29 	struct iova_domain stream_iovad;
30 	struct iova_domain consistent_iovad;
31 	struct vduse_bounce_map *bounce_maps;
32 	size_t bounce_size;
33 	unsigned long iova_limit;
34 	int bounce_map;
35 	struct vhost_iotlb *iotlb;
36 	spinlock_t iotlb_lock;
37 	struct file *file;
38 	bool user_bounce_pages;
39 	rwlock_t bounce_lock;
40 };
41 
42 int vduse_domain_set_map(struct vduse_iova_domain *domain,
43 			 struct vhost_iotlb *iotlb);
44 
45 void vduse_domain_clear_map(struct vduse_iova_domain *domain,
46 			    struct vhost_iotlb *iotlb);
47 
48 void vduse_domain_sync_single_for_device(struct vduse_iova_domain *domain,
49 				      dma_addr_t dma_addr, size_t size,
50 				      enum dma_data_direction dir);
51 
52 void vduse_domain_sync_single_for_cpu(struct vduse_iova_domain *domain,
53 				      dma_addr_t dma_addr, size_t size,
54 				      enum dma_data_direction dir);
55 
56 dma_addr_t vduse_domain_map_page(struct vduse_iova_domain *domain,
57 				 struct page *page, unsigned long offset,
58 				 size_t size, enum dma_data_direction dir,
59 				 unsigned long attrs);
60 
61 void vduse_domain_unmap_page(struct vduse_iova_domain *domain,
62 			     dma_addr_t dma_addr, size_t size,
63 			     enum dma_data_direction dir, unsigned long attrs);
64 
65 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain,
66 				  size_t size, dma_addr_t *dma_addr,
67 				  gfp_t flag, unsigned long attrs);
68 
69 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t size,
70 				void *vaddr, dma_addr_t dma_addr,
71 				unsigned long attrs);
72 
73 void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain);
74 
75 int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain,
76 				       struct page **pages, int count);
77 
78 void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain);
79 
80 void vduse_domain_destroy(struct vduse_iova_domain *domain);
81 
82 struct vduse_iova_domain *vduse_domain_create(unsigned long iova_limit,
83 					      size_t bounce_size);
84 
85 int vduse_domain_init(void);
86 
87 void vduse_domain_exit(void);
88 
89 #endif /* _VDUSE_IOVA_DOMAIN_H */
90