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 #include <linux/rwlock.h> 18 19 #define IOVA_START_PFN 1 20 21 #define INVALID_PHYS_ADDR (~(phys_addr_t)0) 22 23 struct vduse_bounce_map { 24 struct page *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 dma_addr_t vduse_domain_map_page(struct vduse_iova_domain *domain, 49 struct page *page, unsigned long offset, 50 size_t size, enum dma_data_direction dir, 51 unsigned long attrs); 52 53 void vduse_domain_unmap_page(struct vduse_iova_domain *domain, 54 dma_addr_t dma_addr, size_t size, 55 enum dma_data_direction dir, unsigned long attrs); 56 57 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain, 58 size_t size, dma_addr_t *dma_addr, 59 gfp_t flag, unsigned long attrs); 60 61 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t size, 62 void *vaddr, dma_addr_t dma_addr, 63 unsigned long attrs); 64 65 void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain); 66 67 int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain, 68 struct page **pages, int count); 69 70 void vduse_domain_remove_user_bounce_pages(struct vduse_iova_domain *domain); 71 72 void vduse_domain_destroy(struct vduse_iova_domain *domain); 73 74 struct vduse_iova_domain *vduse_domain_create(unsigned long iova_limit, 75 size_t bounce_size); 76 77 int vduse_domain_init(void); 78 79 void vduse_domain_exit(void); 80 81 #endif /* _VDUSE_IOVA_DOMAIN_H */ 82