xref: /linux-6.15/include/linux/host1x.h (revision 90bb7664)
1 /*
2  * Copyright (c) 2009-2013, NVIDIA Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #ifndef __LINUX_HOST1X_H
20 #define __LINUX_HOST1X_H
21 
22 #include <linux/device.h>
23 #include <linux/types.h>
24 
25 enum host1x_class {
26 	HOST1X_CLASS_HOST1X = 0x1,
27 	HOST1X_CLASS_GR2D = 0x51,
28 	HOST1X_CLASS_GR2D_SB = 0x52,
29 	HOST1X_CLASS_VIC = 0x5D,
30 	HOST1X_CLASS_GR3D = 0x60,
31 };
32 
33 struct host1x_client;
34 
35 struct host1x_client_ops {
36 	int (*init)(struct host1x_client *client);
37 	int (*exit)(struct host1x_client *client);
38 };
39 
40 struct host1x_client {
41 	struct list_head list;
42 	struct device *parent;
43 	struct device *dev;
44 
45 	const struct host1x_client_ops *ops;
46 
47 	enum host1x_class class;
48 	struct host1x_channel *channel;
49 
50 	struct host1x_syncpt **syncpts;
51 	unsigned int num_syncpts;
52 };
53 
54 /*
55  * host1x buffer objects
56  */
57 
58 struct host1x_bo;
59 struct sg_table;
60 
61 struct host1x_bo_ops {
62 	struct host1x_bo *(*get)(struct host1x_bo *bo);
63 	void (*put)(struct host1x_bo *bo);
64 	dma_addr_t (*pin)(struct host1x_bo *bo, struct sg_table **sgt);
65 	void (*unpin)(struct host1x_bo *bo, struct sg_table *sgt);
66 	void *(*mmap)(struct host1x_bo *bo);
67 	void (*munmap)(struct host1x_bo *bo, void *addr);
68 	void *(*kmap)(struct host1x_bo *bo, unsigned int pagenum);
69 	void (*kunmap)(struct host1x_bo *bo, unsigned int pagenum, void *addr);
70 };
71 
72 struct host1x_bo {
73 	const struct host1x_bo_ops *ops;
74 };
75 
76 static inline void host1x_bo_init(struct host1x_bo *bo,
77 				  const struct host1x_bo_ops *ops)
78 {
79 	bo->ops = ops;
80 }
81 
82 static inline struct host1x_bo *host1x_bo_get(struct host1x_bo *bo)
83 {
84 	return bo->ops->get(bo);
85 }
86 
87 static inline void host1x_bo_put(struct host1x_bo *bo)
88 {
89 	bo->ops->put(bo);
90 }
91 
92 static inline dma_addr_t host1x_bo_pin(struct host1x_bo *bo,
93 				       struct sg_table **sgt)
94 {
95 	return bo->ops->pin(bo, sgt);
96 }
97 
98 static inline void host1x_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
99 {
100 	bo->ops->unpin(bo, sgt);
101 }
102 
103 static inline void *host1x_bo_mmap(struct host1x_bo *bo)
104 {
105 	return bo->ops->mmap(bo);
106 }
107 
108 static inline void host1x_bo_munmap(struct host1x_bo *bo, void *addr)
109 {
110 	bo->ops->munmap(bo, addr);
111 }
112 
113 static inline void *host1x_bo_kmap(struct host1x_bo *bo, unsigned int pagenum)
114 {
115 	return bo->ops->kmap(bo, pagenum);
116 }
117 
118 static inline void host1x_bo_kunmap(struct host1x_bo *bo,
119 				    unsigned int pagenum, void *addr)
120 {
121 	bo->ops->kunmap(bo, pagenum, addr);
122 }
123 
124 /*
125  * host1x syncpoints
126  */
127 
128 #define HOST1X_SYNCPT_CLIENT_MANAGED	(1 << 0)
129 #define HOST1X_SYNCPT_HAS_BASE		(1 << 1)
130 
131 struct host1x_syncpt_base;
132 struct host1x_syncpt;
133 struct host1x;
134 
135 struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id);
136 u32 host1x_syncpt_id(struct host1x_syncpt *sp);
137 u32 host1x_syncpt_read_min(struct host1x_syncpt *sp);
138 u32 host1x_syncpt_read_max(struct host1x_syncpt *sp);
139 u32 host1x_syncpt_read(struct host1x_syncpt *sp);
140 int host1x_syncpt_incr(struct host1x_syncpt *sp);
141 u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs);
142 int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
143 		       u32 *value);
144 struct host1x_syncpt *host1x_syncpt_request(struct device *dev,
145 					    unsigned long flags);
146 void host1x_syncpt_free(struct host1x_syncpt *sp);
147 
148 struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp);
149 u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base);
150 
151 /*
152  * host1x channel
153  */
154 
155 struct host1x_channel;
156 struct host1x_job;
157 
158 struct host1x_channel *host1x_channel_request(struct device *dev);
159 void host1x_channel_free(struct host1x_channel *channel);
160 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel);
161 void host1x_channel_put(struct host1x_channel *channel);
162 int host1x_job_submit(struct host1x_job *job);
163 
164 /*
165  * host1x job
166  */
167 
168 struct host1x_reloc {
169 	struct {
170 		struct host1x_bo *bo;
171 		unsigned long offset;
172 	} cmdbuf;
173 	struct {
174 		struct host1x_bo *bo;
175 		unsigned long offset;
176 	} target;
177 	unsigned long shift;
178 };
179 
180 struct host1x_job {
181 	/* When refcount goes to zero, job can be freed */
182 	struct kref ref;
183 
184 	/* List entry */
185 	struct list_head list;
186 
187 	/* Channel where job is submitted to */
188 	struct host1x_channel *channel;
189 
190 	u32 client;
191 
192 	/* Gathers and their memory */
193 	struct host1x_job_gather *gathers;
194 	unsigned int num_gathers;
195 
196 	/* Wait checks to be processed at submit time */
197 	struct host1x_waitchk *waitchk;
198 	unsigned int num_waitchk;
199 	u32 waitchk_mask;
200 
201 	/* Array of handles to be pinned & unpinned */
202 	struct host1x_reloc *relocarray;
203 	unsigned int num_relocs;
204 	struct host1x_job_unpin_data *unpins;
205 	unsigned int num_unpins;
206 
207 	dma_addr_t *addr_phys;
208 	dma_addr_t *gather_addr_phys;
209 	dma_addr_t *reloc_addr_phys;
210 
211 	/* Sync point id, number of increments and end related to the submit */
212 	u32 syncpt_id;
213 	u32 syncpt_incrs;
214 	u32 syncpt_end;
215 
216 	/* Maximum time to wait for this job */
217 	unsigned int timeout;
218 
219 	/* Index and number of slots used in the push buffer */
220 	unsigned int first_get;
221 	unsigned int num_slots;
222 
223 	/* Copy of gathers */
224 	size_t gather_copy_size;
225 	dma_addr_t gather_copy;
226 	u8 *gather_copy_mapped;
227 
228 	/* Check if register is marked as an address reg */
229 	int (*is_addr_reg)(struct device *dev, u32 reg, u32 class);
230 
231 	/* Request a SETCLASS to this class */
232 	u32 class;
233 
234 	/* Add a channel wait for previous ops to complete */
235 	bool serialize;
236 };
237 
238 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
239 				    u32 num_cmdbufs, u32 num_relocs,
240 				    u32 num_waitchks);
241 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *mem_id,
242 			   u32 words, u32 offset);
243 struct host1x_job *host1x_job_get(struct host1x_job *job);
244 void host1x_job_put(struct host1x_job *job);
245 int host1x_job_pin(struct host1x_job *job, struct device *dev);
246 void host1x_job_unpin(struct host1x_job *job);
247 
248 /*
249  * subdevice probe infrastructure
250  */
251 
252 struct host1x_device;
253 
254 struct host1x_driver {
255 	struct device_driver driver;
256 
257 	const struct of_device_id *subdevs;
258 	struct list_head list;
259 
260 	int (*probe)(struct host1x_device *device);
261 	int (*remove)(struct host1x_device *device);
262 	void (*shutdown)(struct host1x_device *device);
263 };
264 
265 static inline struct host1x_driver *
266 to_host1x_driver(struct device_driver *driver)
267 {
268 	return container_of(driver, struct host1x_driver, driver);
269 }
270 
271 int host1x_driver_register_full(struct host1x_driver *driver,
272 				struct module *owner);
273 void host1x_driver_unregister(struct host1x_driver *driver);
274 
275 #define host1x_driver_register(driver) \
276 	host1x_driver_register_full(driver, THIS_MODULE)
277 
278 struct host1x_device {
279 	struct host1x_driver *driver;
280 	struct list_head list;
281 	struct device dev;
282 
283 	struct mutex subdevs_lock;
284 	struct list_head subdevs;
285 	struct list_head active;
286 
287 	struct mutex clients_lock;
288 	struct list_head clients;
289 
290 	bool registered;
291 };
292 
293 static inline struct host1x_device *to_host1x_device(struct device *dev)
294 {
295 	return container_of(dev, struct host1x_device, dev);
296 }
297 
298 int host1x_device_init(struct host1x_device *device);
299 int host1x_device_exit(struct host1x_device *device);
300 
301 int host1x_client_register(struct host1x_client *client);
302 int host1x_client_unregister(struct host1x_client *client);
303 
304 struct tegra_mipi_device;
305 
306 struct tegra_mipi_device *tegra_mipi_request(struct device *device);
307 void tegra_mipi_free(struct tegra_mipi_device *device);
308 int tegra_mipi_enable(struct tegra_mipi_device *device);
309 int tegra_mipi_disable(struct tegra_mipi_device *device);
310 int tegra_mipi_calibrate(struct tegra_mipi_device *device);
311 
312 #endif
313