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