xref: /linux-6.15/include/linux/host1x.h (revision 2aed4f5a)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (c) 2009-2013, NVIDIA Corporation. All rights reserved.
4  */
5 
6 #ifndef __LINUX_HOST1X_H
7 #define __LINUX_HOST1X_H
8 
9 #include <linux/device.h>
10 #include <linux/types.h>
11 
12 enum host1x_class {
13 	HOST1X_CLASS_HOST1X = 0x1,
14 	HOST1X_CLASS_GR2D = 0x51,
15 	HOST1X_CLASS_GR2D_SB = 0x52,
16 	HOST1X_CLASS_VIC = 0x5D,
17 	HOST1X_CLASS_GR3D = 0x60,
18 };
19 
20 struct host1x;
21 struct host1x_client;
22 struct iommu_group;
23 
24 u64 host1x_get_dma_mask(struct host1x *host1x);
25 
26 /**
27  * struct host1x_client_ops - host1x client operations
28  * @init: host1x client initialization code
29  * @exit: host1x client tear down code
30  * @suspend: host1x client suspend code
31  * @resume: host1x client resume code
32  */
33 struct host1x_client_ops {
34 	int (*init)(struct host1x_client *client);
35 	int (*exit)(struct host1x_client *client);
36 	int (*suspend)(struct host1x_client *client);
37 	int (*resume)(struct host1x_client *client);
38 };
39 
40 /**
41  * struct host1x_client - host1x client structure
42  * @list: list node for the host1x client
43  * @host: pointer to struct device representing the host1x controller
44  * @dev: pointer to struct device backing this host1x client
45  * @group: IOMMU group that this client is a member of
46  * @ops: host1x client operations
47  * @class: host1x class represented by this client
48  * @channel: host1x channel associated with this client
49  * @syncpts: array of syncpoints requested for this client
50  * @num_syncpts: number of syncpoints requested for this client
51  * @parent: pointer to parent structure
52  * @usecount: reference count for this structure
53  * @lock: mutex for mutually exclusive concurrency
54  */
55 struct host1x_client {
56 	struct list_head list;
57 	struct device *host;
58 	struct device *dev;
59 	struct iommu_group *group;
60 
61 	const struct host1x_client_ops *ops;
62 
63 	enum host1x_class class;
64 	struct host1x_channel *channel;
65 
66 	struct host1x_syncpt **syncpts;
67 	unsigned int num_syncpts;
68 
69 	struct host1x_client *parent;
70 	unsigned int usecount;
71 	struct mutex lock;
72 };
73 
74 /*
75  * host1x buffer objects
76  */
77 
78 struct host1x_bo;
79 struct sg_table;
80 
81 struct host1x_bo_ops {
82 	struct host1x_bo *(*get)(struct host1x_bo *bo);
83 	void (*put)(struct host1x_bo *bo);
84 	struct sg_table *(*pin)(struct device *dev, struct host1x_bo *bo,
85 				dma_addr_t *phys);
86 	void (*unpin)(struct device *dev, struct sg_table *sgt);
87 	void *(*mmap)(struct host1x_bo *bo);
88 	void (*munmap)(struct host1x_bo *bo, void *addr);
89 };
90 
91 struct host1x_bo {
92 	const struct host1x_bo_ops *ops;
93 };
94 
95 static inline void host1x_bo_init(struct host1x_bo *bo,
96 				  const struct host1x_bo_ops *ops)
97 {
98 	bo->ops = ops;
99 }
100 
101 static inline struct host1x_bo *host1x_bo_get(struct host1x_bo *bo)
102 {
103 	return bo->ops->get(bo);
104 }
105 
106 static inline void host1x_bo_put(struct host1x_bo *bo)
107 {
108 	bo->ops->put(bo);
109 }
110 
111 static inline struct sg_table *host1x_bo_pin(struct device *dev,
112 					     struct host1x_bo *bo,
113 					     dma_addr_t *phys)
114 {
115 	return bo->ops->pin(dev, bo, phys);
116 }
117 
118 static inline void host1x_bo_unpin(struct device *dev, struct host1x_bo *bo,
119 				   struct sg_table *sgt)
120 {
121 	bo->ops->unpin(dev, sgt);
122 }
123 
124 static inline void *host1x_bo_mmap(struct host1x_bo *bo)
125 {
126 	return bo->ops->mmap(bo);
127 }
128 
129 static inline void host1x_bo_munmap(struct host1x_bo *bo, void *addr)
130 {
131 	bo->ops->munmap(bo, addr);
132 }
133 
134 /*
135  * host1x syncpoints
136  */
137 
138 #define HOST1X_SYNCPT_CLIENT_MANAGED	(1 << 0)
139 #define HOST1X_SYNCPT_HAS_BASE		(1 << 1)
140 
141 struct host1x_syncpt_base;
142 struct host1x_syncpt;
143 struct host1x;
144 
145 struct host1x_syncpt *host1x_syncpt_get_by_id(struct host1x *host, u32 id);
146 struct host1x_syncpt *host1x_syncpt_get_by_id_noref(struct host1x *host, u32 id);
147 struct host1x_syncpt *host1x_syncpt_get(struct host1x_syncpt *sp);
148 u32 host1x_syncpt_id(struct host1x_syncpt *sp);
149 u32 host1x_syncpt_read_min(struct host1x_syncpt *sp);
150 u32 host1x_syncpt_read_max(struct host1x_syncpt *sp);
151 u32 host1x_syncpt_read(struct host1x_syncpt *sp);
152 int host1x_syncpt_incr(struct host1x_syncpt *sp);
153 u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs);
154 int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
155 		       u32 *value);
156 struct host1x_syncpt *host1x_syncpt_request(struct host1x_client *client,
157 					    unsigned long flags);
158 void host1x_syncpt_put(struct host1x_syncpt *sp);
159 struct host1x_syncpt *host1x_syncpt_alloc(struct host1x *host,
160 					  unsigned long flags,
161 					  const char *name);
162 
163 struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp);
164 u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base);
165 
166 /*
167  * host1x channel
168  */
169 
170 struct host1x_channel;
171 struct host1x_job;
172 
173 struct host1x_channel *host1x_channel_request(struct host1x_client *client);
174 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel);
175 void host1x_channel_put(struct host1x_channel *channel);
176 int host1x_job_submit(struct host1x_job *job);
177 
178 /*
179  * host1x job
180  */
181 
182 #define HOST1X_RELOC_READ	(1 << 0)
183 #define HOST1X_RELOC_WRITE	(1 << 1)
184 
185 struct host1x_reloc {
186 	struct {
187 		struct host1x_bo *bo;
188 		unsigned long offset;
189 	} cmdbuf;
190 	struct {
191 		struct host1x_bo *bo;
192 		unsigned long offset;
193 	} target;
194 	unsigned long shift;
195 	unsigned long flags;
196 };
197 
198 struct host1x_job {
199 	/* When refcount goes to zero, job can be freed */
200 	struct kref ref;
201 
202 	/* List entry */
203 	struct list_head list;
204 
205 	/* Channel where job is submitted to */
206 	struct host1x_channel *channel;
207 
208 	/* client where the job originated */
209 	struct host1x_client *client;
210 
211 	/* Gathers and their memory */
212 	struct host1x_job_gather *gathers;
213 	unsigned int num_gathers;
214 
215 	/* Array of handles to be pinned & unpinned */
216 	struct host1x_reloc *relocs;
217 	unsigned int num_relocs;
218 	struct host1x_job_unpin_data *unpins;
219 	unsigned int num_unpins;
220 
221 	dma_addr_t *addr_phys;
222 	dma_addr_t *gather_addr_phys;
223 	dma_addr_t *reloc_addr_phys;
224 
225 	/* Sync point id, number of increments and end related to the submit */
226 	struct host1x_syncpt *syncpt;
227 	u32 syncpt_incrs;
228 	u32 syncpt_end;
229 
230 	/* Maximum time to wait for this job */
231 	unsigned int timeout;
232 
233 	/* Index and number of slots used in the push buffer */
234 	unsigned int first_get;
235 	unsigned int num_slots;
236 
237 	/* Copy of gathers */
238 	size_t gather_copy_size;
239 	dma_addr_t gather_copy;
240 	u8 *gather_copy_mapped;
241 
242 	/* Check if register is marked as an address reg */
243 	int (*is_addr_reg)(struct device *dev, u32 class, u32 reg);
244 
245 	/* Check if class belongs to the unit */
246 	int (*is_valid_class)(u32 class);
247 
248 	/* Request a SETCLASS to this class */
249 	u32 class;
250 
251 	/* Add a channel wait for previous ops to complete */
252 	bool serialize;
253 };
254 
255 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
256 				    u32 num_cmdbufs, u32 num_relocs);
257 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
258 			   unsigned int words, unsigned int offset);
259 struct host1x_job *host1x_job_get(struct host1x_job *job);
260 void host1x_job_put(struct host1x_job *job);
261 int host1x_job_pin(struct host1x_job *job, struct device *dev);
262 void host1x_job_unpin(struct host1x_job *job);
263 
264 /*
265  * subdevice probe infrastructure
266  */
267 
268 struct host1x_device;
269 
270 /**
271  * struct host1x_driver - host1x logical device driver
272  * @driver: core driver
273  * @subdevs: table of OF device IDs matching subdevices for this driver
274  * @list: list node for the driver
275  * @probe: called when the host1x logical device is probed
276  * @remove: called when the host1x logical device is removed
277  * @shutdown: called when the host1x logical device is shut down
278  */
279 struct host1x_driver {
280 	struct device_driver driver;
281 
282 	const struct of_device_id *subdevs;
283 	struct list_head list;
284 
285 	int (*probe)(struct host1x_device *device);
286 	int (*remove)(struct host1x_device *device);
287 	void (*shutdown)(struct host1x_device *device);
288 };
289 
290 static inline struct host1x_driver *
291 to_host1x_driver(struct device_driver *driver)
292 {
293 	return container_of(driver, struct host1x_driver, driver);
294 }
295 
296 int host1x_driver_register_full(struct host1x_driver *driver,
297 				struct module *owner);
298 void host1x_driver_unregister(struct host1x_driver *driver);
299 
300 #define host1x_driver_register(driver) \
301 	host1x_driver_register_full(driver, THIS_MODULE)
302 
303 struct host1x_device {
304 	struct host1x_driver *driver;
305 	struct list_head list;
306 	struct device dev;
307 
308 	struct mutex subdevs_lock;
309 	struct list_head subdevs;
310 	struct list_head active;
311 
312 	struct mutex clients_lock;
313 	struct list_head clients;
314 
315 	bool registered;
316 
317 	struct device_dma_parameters dma_parms;
318 };
319 
320 static inline struct host1x_device *to_host1x_device(struct device *dev)
321 {
322 	return container_of(dev, struct host1x_device, dev);
323 }
324 
325 int host1x_device_init(struct host1x_device *device);
326 int host1x_device_exit(struct host1x_device *device);
327 
328 int __host1x_client_register(struct host1x_client *client,
329 			     struct lock_class_key *key);
330 #define host1x_client_register(class) \
331 	({ \
332 		static struct lock_class_key __key; \
333 		__host1x_client_register(class, &__key); \
334 	})
335 
336 int host1x_client_unregister(struct host1x_client *client);
337 
338 int host1x_client_suspend(struct host1x_client *client);
339 int host1x_client_resume(struct host1x_client *client);
340 
341 struct tegra_mipi_device;
342 
343 struct tegra_mipi_device *tegra_mipi_request(struct device *device,
344 					     struct device_node *np);
345 void tegra_mipi_free(struct tegra_mipi_device *device);
346 int tegra_mipi_enable(struct tegra_mipi_device *device);
347 int tegra_mipi_disable(struct tegra_mipi_device *device);
348 int tegra_mipi_start_calibration(struct tegra_mipi_device *device);
349 int tegra_mipi_finish_calibration(struct tegra_mipi_device *device);
350 
351 #endif
352