1 /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2 /*
3 * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4 *
5 * author:
6 * Alpha Lin, alpha.lin@rock-chips.com
7 * Randy Li, randy.li@rock-chips.com
8 * Ding Wei, leo.ding@rock-chips.com
9 *
10 */
11 #ifndef __ROCKCHIP_MPP_IOMMU_H__
12 #define __ROCKCHIP_MPP_IOMMU_H__
13
14 #include <linux/iommu.h>
15 #include <linux/dma-mapping.h>
16
17 struct mpp_dma_buffer {
18 /* link to dma session buffer list */
19 struct list_head link;
20
21 /* dma session belong */
22 struct mpp_dma_session *dma;
23 /* DMABUF information */
24 struct dma_buf *dmabuf;
25 struct dma_buf_attachment *attach;
26 struct sg_table *sgt;
27 struct sg_table *copy_sgt;
28 enum dma_data_direction dir;
29
30 dma_addr_t iova;
31 unsigned long size;
32 void *vaddr;
33
34 struct kref ref;
35 ktime_t last_used;
36 /* alloc by device */
37 struct device *dev;
38 };
39
40 #define MPP_SESSION_MAX_BUFFERS 60
41
42 struct mpp_dma_session {
43 /* the buffer used in session */
44 struct list_head unused_list;
45 struct list_head used_list;
46 struct mpp_dma_buffer dma_bufs[MPP_SESSION_MAX_BUFFERS];
47 /* the mutex for the above buffer list */
48 struct mutex list_mutex;
49 /* the max buffer num for the buffer list */
50 u32 max_buffers;
51 /* the count for the buffer list */
52 int buffer_count;
53
54 struct device *dev;
55 };
56
57 struct mpp_rk_iommu {
58 struct list_head link;
59 u32 grf_val;
60 int mmu_num;
61 u32 base_addr[2];
62 void __iomem *bases[2];
63 u32 dte_addr;
64 u32 is_paged;
65 };
66
67 struct mpp_iommu_info {
68 struct rw_semaphore rw_sem;
69
70 struct device *dev;
71 struct platform_device *pdev;
72 struct iommu_domain *domain;
73 struct iommu_group *group;
74 struct mpp_rk_iommu *iommu;
75 iommu_fault_handler_t hdl;
76 u32 skip_refresh;
77 };
78
79 struct mpp_dma_session *
80 mpp_dma_session_create(struct device *dev, u32 max_buffers);
81 int mpp_dma_session_destroy(struct mpp_dma_session *dma);
82
83 struct mpp_dma_buffer *
84 mpp_dma_alloc(struct device *dev, size_t size);
85 int mpp_dma_free(struct mpp_dma_buffer *buffer);
86
87 struct mpp_dma_buffer *
88 mpp_dma_import_fd(struct mpp_iommu_info *iommu_info,
89 struct mpp_dma_session *dma, int fd);
90 int mpp_dma_release(struct mpp_dma_session *dma,
91 struct mpp_dma_buffer *buffer);
92 int mpp_dma_release_fd(struct mpp_dma_session *dma, int fd);
93
94 int mpp_dma_unmap_kernel(struct mpp_dma_session *dma,
95 struct mpp_dma_buffer *buffer);
96 int mpp_dma_map_kernel(struct mpp_dma_session *dma,
97 struct mpp_dma_buffer *buffer);
98
99 struct mpp_iommu_info *
100 mpp_iommu_probe(struct device *dev);
101 int mpp_iommu_remove(struct mpp_iommu_info *info);
102
103 int mpp_iommu_attach(struct mpp_iommu_info *info);
104 int mpp_iommu_detach(struct mpp_iommu_info *info);
105
106 int mpp_iommu_refresh(struct mpp_iommu_info *info, struct device *dev);
107 int mpp_iommu_flush_tlb(struct mpp_iommu_info *info);
108
mpp_iommu_down_read(struct mpp_iommu_info * info)109 static inline int mpp_iommu_down_read(struct mpp_iommu_info *info)
110 {
111 if (info)
112 down_read(&info->rw_sem);
113
114 return 0;
115 }
116
mpp_iommu_up_read(struct mpp_iommu_info * info)117 static inline int mpp_iommu_up_read(struct mpp_iommu_info *info)
118 {
119 if (info)
120 up_read(&info->rw_sem);
121
122 return 0;
123 }
124
mpp_iommu_down_write(struct mpp_iommu_info * info)125 static inline int mpp_iommu_down_write(struct mpp_iommu_info *info)
126 {
127 if (info)
128 down_write(&info->rw_sem);
129
130 return 0;
131 }
132
mpp_iommu_up_write(struct mpp_iommu_info * info)133 static inline int mpp_iommu_up_write(struct mpp_iommu_info *info)
134 {
135 if (info)
136 up_write(&info->rw_sem);
137
138 return 0;
139 }
140
141 #endif
142