xref: /OK3568_Linux_fs/kernel/drivers/video/rockchip/mpp/mpp_iommu.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #include <linux/interrupt.h>
17 
18 struct mpp_dma_buffer {
19 	/* link to dma session buffer list */
20 	struct list_head link;
21 
22 	/* dma session belong */
23 	struct mpp_dma_session *dma;
24 	/* DMABUF information */
25 	struct dma_buf *dmabuf;
26 	struct dma_buf_attachment *attach;
27 	struct sg_table *sgt;
28 	struct sg_table *copy_sgt;
29 	enum dma_data_direction dir;
30 
31 	dma_addr_t iova;
32 	unsigned long size;
33 	void *vaddr;
34 
35 	struct kref ref;
36 	ktime_t last_used;
37 	/* alloc by device */
38 	struct device *dev;
39 };
40 
41 #define MPP_SESSION_MAX_BUFFERS		60
42 
43 struct mpp_dma_session {
44 	/* the buffer used in session */
45 	struct list_head unused_list;
46 	struct list_head used_list;
47 	struct mpp_dma_buffer dma_bufs[MPP_SESSION_MAX_BUFFERS];
48 	/* the mutex for the above buffer list */
49 	struct mutex list_mutex;
50 	/* the max buffer num for the buffer list */
51 	u32 max_buffers;
52 	/* the count for the buffer list */
53 	int buffer_count;
54 
55 	struct device *dev;
56 };
57 
58 struct mpp_rk_iommu {
59 	struct list_head link;
60 	u32 grf_val;
61 	int mmu_num;
62 	u32 base_addr[2];
63 	void __iomem *bases[2];
64 	u32 dte_addr;
65 	u32 is_paged;
66 };
67 
68 struct mpp_dev;
69 
70 struct mpp_iommu_info {
71 	struct rw_semaphore rw_sem;
72 
73 	struct device *dev;
74 	struct platform_device *pdev;
75 	struct iommu_domain *domain;
76 	struct iommu_group *group;
77 	struct mpp_rk_iommu *iommu;
78 	iommu_fault_handler_t hdl;
79 
80 	spinlock_t dev_lock;
81 	struct mpp_dev *dev_active;
82 
83 	u32 av1d_iommu;
84 	int irq;
85 	int got_irq;
86 };
87 
88 struct mpp_dma_session *
89 mpp_dma_session_create(struct device *dev, u32 max_buffers);
90 int mpp_dma_session_destroy(struct mpp_dma_session *dma);
91 
92 struct mpp_dma_buffer *
93 mpp_dma_alloc(struct device *dev, size_t size);
94 int mpp_dma_free(struct mpp_dma_buffer *buffer);
95 
96 struct mpp_dma_buffer *
97 mpp_dma_import_fd(struct mpp_iommu_info *iommu_info,
98 		  struct mpp_dma_session *dma, int fd);
99 int mpp_dma_release(struct mpp_dma_session *dma,
100 		    struct mpp_dma_buffer *buffer);
101 int mpp_dma_release_fd(struct mpp_dma_session *dma, int fd);
102 
103 int mpp_dma_unmap_kernel(struct mpp_dma_session *dma,
104 			 struct mpp_dma_buffer *buffer);
105 int mpp_dma_map_kernel(struct mpp_dma_session *dma,
106 		       struct mpp_dma_buffer *buffer);
107 struct mpp_dma_buffer *mpp_dma_find_buffer_fd(struct mpp_dma_session *dma, int fd);
108 void mpp_dma_buf_sync(struct mpp_dma_buffer *buffer, u32 offset, u32 length,
109 		      enum dma_data_direction dir, bool for_cpu);
110 
111 struct mpp_iommu_info *
112 mpp_iommu_probe(struct device *dev);
113 int mpp_iommu_remove(struct mpp_iommu_info *info);
114 
115 int mpp_iommu_attach(struct mpp_iommu_info *info);
116 int mpp_iommu_detach(struct mpp_iommu_info *info);
117 
118 int mpp_iommu_refresh(struct mpp_iommu_info *info, struct device *dev);
119 int mpp_iommu_flush_tlb(struct mpp_iommu_info *info);
120 int mpp_av1_iommu_disable(struct device *dev);
121 int mpp_av1_iommu_enable(struct device *dev);
122 
123 int mpp_iommu_dev_activate(struct mpp_iommu_info *info, struct mpp_dev *dev);
124 int mpp_iommu_dev_deactivate(struct mpp_iommu_info *info, struct mpp_dev *dev);
125 
mpp_iommu_down_read(struct mpp_iommu_info * info)126 static inline int mpp_iommu_down_read(struct mpp_iommu_info *info)
127 {
128 	if (info)
129 		down_read(&info->rw_sem);
130 
131 	return 0;
132 }
133 
mpp_iommu_up_read(struct mpp_iommu_info * info)134 static inline int mpp_iommu_up_read(struct mpp_iommu_info *info)
135 {
136 	if (info)
137 		up_read(&info->rw_sem);
138 
139 	return 0;
140 }
141 
mpp_iommu_down_write(struct mpp_iommu_info * info)142 static inline int mpp_iommu_down_write(struct mpp_iommu_info *info)
143 {
144 	if (info)
145 		down_write(&info->rw_sem);
146 
147 	return 0;
148 }
149 
mpp_iommu_up_write(struct mpp_iommu_info * info)150 static inline int mpp_iommu_up_write(struct mpp_iommu_info *info)
151 {
152 	if (info)
153 		up_write(&info->rw_sem);
154 
155 	return 0;
156 }
157 
mpp_iommu_enable_irq(struct mpp_iommu_info * info)158 static inline void mpp_iommu_enable_irq(struct mpp_iommu_info *info)
159 {
160 	if (info && info->got_irq)
161 		enable_irq(info->irq);
162 }
163 
mpp_iommu_disable_irq(struct mpp_iommu_info * info)164 static inline void mpp_iommu_disable_irq(struct mpp_iommu_info *info)
165 {
166 	if (info && info->got_irq)
167 		disable_irq(info->irq);
168 }
169 
170 #endif
171