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_client;
21 
22 /**
23  * struct host1x_client_ops - host1x client operations
24  * @init: host1x client initialization code
25  * @exit: host1x client tear down code
26  */
27 struct host1x_client_ops {
28 	int (*init)(struct host1x_client *client);
29 	int (*exit)(struct host1x_client *client);
30 };
31 
32 /**
33  * struct host1x_client - host1x client structure
34  * @list: list node for the host1x client
35  * @parent: pointer to struct device representing the host1x controller
36  * @dev: pointer to struct device backing this host1x client
37  * @ops: host1x client operations
38  * @class: host1x class represented by this client
39  * @channel: host1x channel associated with this client
40  * @syncpts: array of syncpoints requested for this client
41  * @num_syncpts: number of syncpoints requested for this client
42  */
43 struct host1x_client {
44 	struct list_head list;
45 	struct device *parent;
46 	struct device *dev;
47 
48 	const struct host1x_client_ops *ops;
49 
50 	enum host1x_class class;
51 	struct host1x_channel *channel;
52 
53 	struct host1x_syncpt **syncpts;
54 	unsigned int num_syncpts;
55 };
56 
57 /*
58  * host1x buffer objects
59  */
60 
61 struct host1x_bo;
62 struct sg_table;
63 
64 struct host1x_bo_ops {
65 	struct host1x_bo *(*get)(struct host1x_bo *bo);
66 	void (*put)(struct host1x_bo *bo);
67 	dma_addr_t (*pin)(struct host1x_bo *bo, struct sg_table **sgt);
68 	void (*unpin)(struct host1x_bo *bo, struct sg_table *sgt);
69 	void *(*mmap)(struct host1x_bo *bo);
70 	void (*munmap)(struct host1x_bo *bo, void *addr);
71 	void *(*kmap)(struct host1x_bo *bo, unsigned int pagenum);
72 	void (*kunmap)(struct host1x_bo *bo, unsigned int pagenum, void *addr);
73 };
74 
75 struct host1x_bo {
76 	const struct host1x_bo_ops *ops;
77 };
78 
host1x_bo_init(struct host1x_bo * bo,const struct host1x_bo_ops * ops)79 static inline void host1x_bo_init(struct host1x_bo *bo,
80 				  const struct host1x_bo_ops *ops)
81 {
82 	bo->ops = ops;
83 }
84 
host1x_bo_get(struct host1x_bo * bo)85 static inline struct host1x_bo *host1x_bo_get(struct host1x_bo *bo)
86 {
87 	return bo->ops->get(bo);
88 }
89 
host1x_bo_put(struct host1x_bo * bo)90 static inline void host1x_bo_put(struct host1x_bo *bo)
91 {
92 	bo->ops->put(bo);
93 }
94 
host1x_bo_pin(struct host1x_bo * bo,struct sg_table ** sgt)95 static inline dma_addr_t host1x_bo_pin(struct host1x_bo *bo,
96 				       struct sg_table **sgt)
97 {
98 	return bo->ops->pin(bo, sgt);
99 }
100 
host1x_bo_unpin(struct host1x_bo * bo,struct sg_table * sgt)101 static inline void host1x_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
102 {
103 	bo->ops->unpin(bo, sgt);
104 }
105 
host1x_bo_mmap(struct host1x_bo * bo)106 static inline void *host1x_bo_mmap(struct host1x_bo *bo)
107 {
108 	return bo->ops->mmap(bo);
109 }
110 
host1x_bo_munmap(struct host1x_bo * bo,void * addr)111 static inline void host1x_bo_munmap(struct host1x_bo *bo, void *addr)
112 {
113 	bo->ops->munmap(bo, addr);
114 }
115 
host1x_bo_kmap(struct host1x_bo * bo,unsigned int pagenum)116 static inline void *host1x_bo_kmap(struct host1x_bo *bo, unsigned int pagenum)
117 {
118 	return bo->ops->kmap(bo, pagenum);
119 }
120 
host1x_bo_kunmap(struct host1x_bo * bo,unsigned int pagenum,void * addr)121 static inline void host1x_bo_kunmap(struct host1x_bo *bo,
122 				    unsigned int pagenum, void *addr)
123 {
124 	bo->ops->kunmap(bo, pagenum, addr);
125 }
126 
127 /*
128  * host1x syncpoints
129  */
130 
131 #define HOST1X_SYNCPT_CLIENT_MANAGED	(1 << 0)
132 #define HOST1X_SYNCPT_HAS_BASE		(1 << 1)
133 
134 struct host1x_syncpt_base;
135 struct host1x_syncpt;
136 struct host1x;
137 
138 struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id);
139 u32 host1x_syncpt_id(struct host1x_syncpt *sp);
140 u32 host1x_syncpt_read_min(struct host1x_syncpt *sp);
141 u32 host1x_syncpt_read_max(struct host1x_syncpt *sp);
142 u32 host1x_syncpt_read(struct host1x_syncpt *sp);
143 int host1x_syncpt_incr(struct host1x_syncpt *sp);
144 u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs);
145 int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
146 		       u32 *value);
147 struct host1x_syncpt *host1x_syncpt_request(struct host1x_client *client,
148 					    unsigned long flags);
149 void host1x_syncpt_free(struct host1x_syncpt *sp);
150 
151 struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp);
152 u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base);
153 
154 /*
155  * host1x channel
156  */
157 
158 struct host1x_channel;
159 struct host1x_job;
160 
161 struct host1x_channel *host1x_channel_request(struct device *dev);
162 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel);
163 void host1x_channel_put(struct host1x_channel *channel);
164 int host1x_job_submit(struct host1x_job *job);
165 
166 /*
167  * host1x job
168  */
169 
170 struct host1x_reloc {
171 	struct {
172 		struct host1x_bo *bo;
173 		unsigned long offset;
174 	} cmdbuf;
175 	struct {
176 		struct host1x_bo *bo;
177 		unsigned long offset;
178 	} target;
179 	unsigned long shift;
180 };
181 
182 struct host1x_job {
183 	/* When refcount goes to zero, job can be freed */
184 	struct kref ref;
185 
186 	/* List entry */
187 	struct list_head list;
188 
189 	/* Channel where job is submitted to */
190 	struct host1x_channel *channel;
191 
192 	/* client where the job originated */
193 	struct host1x_client *client;
194 
195 	/* Gathers and their memory */
196 	struct host1x_job_gather *gathers;
197 	unsigned int num_gathers;
198 
199 	/* Array of handles to be pinned & unpinned */
200 	struct host1x_reloc *relocs;
201 	unsigned int num_relocs;
202 	struct host1x_job_unpin_data *unpins;
203 	unsigned int num_unpins;
204 
205 	dma_addr_t *addr_phys;
206 	dma_addr_t *gather_addr_phys;
207 	dma_addr_t *reloc_addr_phys;
208 
209 	/* Sync point id, number of increments and end related to the submit */
210 	u32 syncpt_id;
211 	u32 syncpt_incrs;
212 	u32 syncpt_end;
213 
214 	/* Maximum time to wait for this job */
215 	unsigned int timeout;
216 
217 	/* Index and number of slots used in the push buffer */
218 	unsigned int first_get;
219 	unsigned int num_slots;
220 
221 	/* Copy of gathers */
222 	size_t gather_copy_size;
223 	dma_addr_t gather_copy;
224 	u8 *gather_copy_mapped;
225 
226 	/* Check if register is marked as an address reg */
227 	int (*is_addr_reg)(struct device *dev, u32 class, u32 reg);
228 
229 	/* Check if class belongs to the unit */
230 	int (*is_valid_class)(u32 class);
231 
232 	/* Request a SETCLASS to this class */
233 	u32 class;
234 
235 	/* Add a channel wait for previous ops to complete */
236 	bool serialize;
237 };
238 
239 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
240 				    u32 num_cmdbufs, u32 num_relocs);
241 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
242 			   unsigned int words, unsigned int 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 /**
255  * struct host1x_driver - host1x logical device driver
256  * @driver: core driver
257  * @subdevs: table of OF device IDs matching subdevices for this driver
258  * @list: list node for the driver
259  * @probe: called when the host1x logical device is probed
260  * @remove: called when the host1x logical device is removed
261  * @shutdown: called when the host1x logical device is shut down
262  */
263 struct host1x_driver {
264 	struct device_driver driver;
265 
266 	const struct of_device_id *subdevs;
267 	struct list_head list;
268 
269 	int (*probe)(struct host1x_device *device);
270 	int (*remove)(struct host1x_device *device);
271 	void (*shutdown)(struct host1x_device *device);
272 };
273 
274 static inline struct host1x_driver *
to_host1x_driver(struct device_driver * driver)275 to_host1x_driver(struct device_driver *driver)
276 {
277 	return container_of(driver, struct host1x_driver, driver);
278 }
279 
280 int host1x_driver_register_full(struct host1x_driver *driver,
281 				struct module *owner);
282 void host1x_driver_unregister(struct host1x_driver *driver);
283 
284 #define host1x_driver_register(driver) \
285 	host1x_driver_register_full(driver, THIS_MODULE)
286 
287 struct host1x_device {
288 	struct host1x_driver *driver;
289 	struct list_head list;
290 	struct device dev;
291 
292 	struct mutex subdevs_lock;
293 	struct list_head subdevs;
294 	struct list_head active;
295 
296 	struct mutex clients_lock;
297 	struct list_head clients;
298 
299 	bool registered;
300 
301 	struct device_dma_parameters dma_parms;
302 };
303 
to_host1x_device(struct device * dev)304 static inline struct host1x_device *to_host1x_device(struct device *dev)
305 {
306 	return container_of(dev, struct host1x_device, dev);
307 }
308 
309 int host1x_device_init(struct host1x_device *device);
310 int host1x_device_exit(struct host1x_device *device);
311 
312 int host1x_client_register(struct host1x_client *client);
313 int host1x_client_unregister(struct host1x_client *client);
314 
315 struct tegra_mipi_device;
316 
317 struct tegra_mipi_device *tegra_mipi_request(struct device *device);
318 void tegra_mipi_free(struct tegra_mipi_device *device);
319 int tegra_mipi_enable(struct tegra_mipi_device *device);
320 int tegra_mipi_disable(struct tegra_mipi_device *device);
321 int tegra_mipi_calibrate(struct tegra_mipi_device *device);
322 
323 #endif
324