summaryrefslogtreecommitdiffstats
path: root/drivers/vdpa/vdpa_user/iova_domain.h
blob: 7f3f0928ec78141cfe4282f100b1d9392c65fd0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * MMU-based software IOTLB.
 *
 * Copyright (C) 2020-2021 Bytedance Inc. and/or its affiliates. All rights reserved.
 *
 * Author: Xie Yongji <xieyongji@bytedance.com>
 *
 */

#ifndef _VDUSE_IOVA_DOMAIN_H
#define _VDUSE_IOVA_DOMAIN_H

#include <linux/iova.h>
#include <linux/dma-mapping.h>
#include <linux/vhost_iotlb.h>

#define IOVA_START_PFN 1

#define INVALID_PHYS_ADDR (~(phys_addr_t)0)

struct vduse_bounce_map {
	struct page *bounce_page;
	struct page *user_bounce_page;
	u64 orig_phys;
};

struct vduse_iova_domain {
	struct iova_domain stream_iovad;
	struct iova_domain consistent_iovad;
	struct vduse_bounce_map *bounce_maps;
	size_t bounce_size;
	unsigned long iova_limit;
	int bounce_map;
	struct vhost_iotlb *iotlb;
	spinlock_t iotlb_lock;
	struct file *file;
	bool user_bounce_pages;
	rwlock_t bounce_lock;
};

int vduse_domain_set_map(struct vduse_iova_domain *domain,
			 struct vhost_iotlb *iotlb);

void vduse_domain_clear_map(struct vduse_iova_domain *domain,
			    struct vhost_iotlb *iotlb);

void vduse_domain_sync_single_for_device(struct vduse_iova_domain *domain,
				      dma_addr_t dma_addr, size_t size,
				      enum dma_data_direction dir);

void vduse_domain_sync_single_for_cpu(struct vduse_iova_domain *domain,
				      dma_addr_t dma_addr, size_t size,
				      enum dma_data_direction dir);

dma_addr_t vduse_domain_map_page(struct vduse_iova_domain *domain,
				 struct page *page, unsigned long offset,
				 size_t size, enum dma_data_direction dir,
				 unsigned long attrs);

void vduse_domain_unmap_page(struct vduse_iova_domain *domain,
			     dma_addr_t dma_addr, size_t size,
			     enum dma_data_direction dir, unsigned long attrs);

void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain,
				  size_t size, dma_addr_t *dma_addr,
				  gfp_t flag, unsigned long attrs);

void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t size,
				void *vaddr, dma_addr_t dma_addr,
				unsigned long attrs);

void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain);

int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain,
				       struct page **pages, int count);

void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain);

void vduse_domain_destroy(struct vduse_iova_domain *domain);

struct vduse_iova_domain *vduse_domain_create(unsigned long iova_limit,
					      size_t bounce_size);

int vduse_domain_init(void);

void vduse_domain_exit(void);

#endif /* _VDUSE_IOVA_DOMAIN_H */