1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2009-2013, NVIDIA Corporation. All rights reserved.
6 #ifndef __LINUX_HOST1X_H
7 #define __LINUX_HOST1X_H
9 #include <linux/device.h>
10 #include <linux/dma-direction.h>
11 #include <linux/spinlock.h>
12 #include <linux/types.h>
15 HOST1X_CLASS_HOST1X = 0x1,
16 HOST1X_CLASS_GR2D = 0x51,
17 HOST1X_CLASS_GR2D_SB = 0x52,
18 HOST1X_CLASS_VIC = 0x5D,
19 HOST1X_CLASS_GR3D = 0x60,
20 HOST1X_CLASS_NVDEC = 0xF0,
21 HOST1X_CLASS_NVDEC1 = 0xF5,
28 u64 host1x_get_dma_mask(struct host1x *host1x);
31 * struct host1x_bo_cache - host1x buffer object cache
32 * @mappings: list of mappings
33 * @lock: synchronizes accesses to the list of mappings
35 * Note that entries are not periodically evicted from this cache and instead need to be
36 * explicitly released. This is used primarily for DRM/KMS where the cache's reference is
37 * released when the last reference to a buffer object represented by a mapping in this
40 struct host1x_bo_cache {
41 struct list_head mappings;
45 static inline void host1x_bo_cache_init(struct host1x_bo_cache *cache)
47 INIT_LIST_HEAD(&cache->mappings);
48 mutex_init(&cache->lock);
51 static inline void host1x_bo_cache_destroy(struct host1x_bo_cache *cache)
53 /* XXX warn if not empty? */
54 mutex_destroy(&cache->lock);
58 * struct host1x_client_ops - host1x client operations
59 * @early_init: host1x client early initialization code
60 * @init: host1x client initialization code
61 * @exit: host1x client tear down code
62 * @late_exit: host1x client late tear down code
63 * @suspend: host1x client suspend code
64 * @resume: host1x client resume code
66 struct host1x_client_ops {
67 int (*early_init)(struct host1x_client *client);
68 int (*init)(struct host1x_client *client);
69 int (*exit)(struct host1x_client *client);
70 int (*late_exit)(struct host1x_client *client);
71 int (*suspend)(struct host1x_client *client);
72 int (*resume)(struct host1x_client *client);
76 * struct host1x_client - host1x client structure
77 * @list: list node for the host1x client
78 * @host: pointer to struct device representing the host1x controller
79 * @dev: pointer to struct device backing this host1x client
80 * @group: IOMMU group that this client is a member of
81 * @ops: host1x client operations
82 * @class: host1x class represented by this client
83 * @channel: host1x channel associated with this client
84 * @syncpts: array of syncpoints requested for this client
85 * @num_syncpts: number of syncpoints requested for this client
86 * @parent: pointer to parent structure
87 * @usecount: reference count for this structure
88 * @lock: mutex for mutually exclusive concurrency
89 * @cache: host1x buffer object cache
91 struct host1x_client {
92 struct list_head list;
95 struct iommu_group *group;
97 const struct host1x_client_ops *ops;
99 enum host1x_class class;
100 struct host1x_channel *channel;
102 struct host1x_syncpt **syncpts;
103 unsigned int num_syncpts;
105 struct host1x_client *parent;
106 unsigned int usecount;
109 struct host1x_bo_cache cache;
113 * host1x buffer objects
119 struct host1x_bo_mapping {
121 struct dma_buf_attachment *attach;
122 enum dma_data_direction direction;
123 struct list_head list;
124 struct host1x_bo *bo;
125 struct sg_table *sgt;
131 struct host1x_bo_cache *cache;
132 struct list_head entry;
135 static inline struct host1x_bo_mapping *to_host1x_bo_mapping(struct kref *ref)
137 return container_of(ref, struct host1x_bo_mapping, ref);
140 struct host1x_bo_ops {
141 struct host1x_bo *(*get)(struct host1x_bo *bo);
142 void (*put)(struct host1x_bo *bo);
143 struct host1x_bo_mapping *(*pin)(struct device *dev, struct host1x_bo *bo,
144 enum dma_data_direction dir);
145 void (*unpin)(struct host1x_bo_mapping *map);
146 void *(*mmap)(struct host1x_bo *bo);
147 void (*munmap)(struct host1x_bo *bo, void *addr);
151 const struct host1x_bo_ops *ops;
152 struct list_head mappings;
156 static inline void host1x_bo_init(struct host1x_bo *bo,
157 const struct host1x_bo_ops *ops)
159 INIT_LIST_HEAD(&bo->mappings);
160 spin_lock_init(&bo->lock);
164 static inline struct host1x_bo *host1x_bo_get(struct host1x_bo *bo)
166 return bo->ops->get(bo);
169 static inline void host1x_bo_put(struct host1x_bo *bo)
174 struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo,
175 enum dma_data_direction dir,
176 struct host1x_bo_cache *cache);
177 void host1x_bo_unpin(struct host1x_bo_mapping *map);
179 static inline void *host1x_bo_mmap(struct host1x_bo *bo)
181 return bo->ops->mmap(bo);
184 static inline void host1x_bo_munmap(struct host1x_bo *bo, void *addr)
186 bo->ops->munmap(bo, addr);
193 #define HOST1X_SYNCPT_CLIENT_MANAGED (1 << 0)
194 #define HOST1X_SYNCPT_HAS_BASE (1 << 1)
196 struct host1x_syncpt_base;
197 struct host1x_syncpt;
200 struct host1x_syncpt *host1x_syncpt_get_by_id(struct host1x *host, u32 id);
201 struct host1x_syncpt *host1x_syncpt_get_by_id_noref(struct host1x *host, u32 id);
202 struct host1x_syncpt *host1x_syncpt_get(struct host1x_syncpt *sp);
203 u32 host1x_syncpt_id(struct host1x_syncpt *sp);
204 u32 host1x_syncpt_read_min(struct host1x_syncpt *sp);
205 u32 host1x_syncpt_read_max(struct host1x_syncpt *sp);
206 u32 host1x_syncpt_read(struct host1x_syncpt *sp);
207 int host1x_syncpt_incr(struct host1x_syncpt *sp);
208 u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs);
209 int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
211 struct host1x_syncpt *host1x_syncpt_request(struct host1x_client *client,
212 unsigned long flags);
213 void host1x_syncpt_put(struct host1x_syncpt *sp);
214 struct host1x_syncpt *host1x_syncpt_alloc(struct host1x *host,
218 struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp);
219 u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base);
221 void host1x_syncpt_release_vblank_reservation(struct host1x_client *client,
224 struct dma_fence *host1x_fence_create(struct host1x_syncpt *sp, u32 threshold);
230 struct host1x_channel;
233 struct host1x_channel *host1x_channel_request(struct host1x_client *client);
234 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel);
235 void host1x_channel_stop(struct host1x_channel *channel);
236 void host1x_channel_put(struct host1x_channel *channel);
237 int host1x_job_submit(struct host1x_job *job);
243 #define HOST1X_RELOC_READ (1 << 0)
244 #define HOST1X_RELOC_WRITE (1 << 1)
246 struct host1x_reloc {
248 struct host1x_bo *bo;
249 unsigned long offset;
252 struct host1x_bo *bo;
253 unsigned long offset;
260 /* When refcount goes to zero, job can be freed */
264 struct list_head list;
266 /* Channel where job is submitted to */
267 struct host1x_channel *channel;
269 /* client where the job originated */
270 struct host1x_client *client;
272 /* Gathers and their memory */
273 struct host1x_job_cmd *cmds;
274 unsigned int num_cmds;
276 /* Array of handles to be pinned & unpinned */
277 struct host1x_reloc *relocs;
278 unsigned int num_relocs;
279 struct host1x_job_unpin_data *unpins;
280 unsigned int num_unpins;
282 dma_addr_t *addr_phys;
283 dma_addr_t *gather_addr_phys;
284 dma_addr_t *reloc_addr_phys;
286 /* Sync point id, number of increments and end related to the submit */
287 struct host1x_syncpt *syncpt;
291 /* Completion waiter ref */
294 /* Maximum time to wait for this job */
295 unsigned int timeout;
297 /* Job has timed out and should be released */
300 /* Index and number of slots used in the push buffer */
301 unsigned int first_get;
302 unsigned int num_slots;
304 /* Copy of gathers */
305 size_t gather_copy_size;
306 dma_addr_t gather_copy;
307 u8 *gather_copy_mapped;
309 /* Check if register is marked as an address reg */
310 int (*is_addr_reg)(struct device *dev, u32 class, u32 reg);
312 /* Check if class belongs to the unit */
313 int (*is_valid_class)(u32 class);
315 /* Request a SETCLASS to this class */
318 /* Add a channel wait for previous ops to complete */
321 /* Fast-forward syncpoint increments on job timeout */
322 bool syncpt_recovery;
324 /* Callback called when job is freed */
325 void (*release)(struct host1x_job *job);
328 /* Whether host1x-side firewall should be ran for this job or not */
329 bool enable_firewall;
332 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
333 u32 num_cmdbufs, u32 num_relocs,
335 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
336 unsigned int words, unsigned int offset);
337 void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
338 bool relative, u32 next_class);
339 struct host1x_job *host1x_job_get(struct host1x_job *job);
340 void host1x_job_put(struct host1x_job *job);
341 int host1x_job_pin(struct host1x_job *job, struct device *dev);
342 void host1x_job_unpin(struct host1x_job *job);
345 * subdevice probe infrastructure
348 struct host1x_device;
351 * struct host1x_driver - host1x logical device driver
352 * @driver: core driver
353 * @subdevs: table of OF device IDs matching subdevices for this driver
354 * @list: list node for the driver
355 * @probe: called when the host1x logical device is probed
356 * @remove: called when the host1x logical device is removed
357 * @shutdown: called when the host1x logical device is shut down
359 struct host1x_driver {
360 struct device_driver driver;
362 const struct of_device_id *subdevs;
363 struct list_head list;
365 int (*probe)(struct host1x_device *device);
366 int (*remove)(struct host1x_device *device);
367 void (*shutdown)(struct host1x_device *device);
370 static inline struct host1x_driver *
371 to_host1x_driver(struct device_driver *driver)
373 return container_of(driver, struct host1x_driver, driver);
376 int host1x_driver_register_full(struct host1x_driver *driver,
377 struct module *owner);
378 void host1x_driver_unregister(struct host1x_driver *driver);
380 #define host1x_driver_register(driver) \
381 host1x_driver_register_full(driver, THIS_MODULE)
383 struct host1x_device {
384 struct host1x_driver *driver;
385 struct list_head list;
388 struct mutex subdevs_lock;
389 struct list_head subdevs;
390 struct list_head active;
392 struct mutex clients_lock;
393 struct list_head clients;
397 struct device_dma_parameters dma_parms;
400 static inline struct host1x_device *to_host1x_device(struct device *dev)
402 return container_of(dev, struct host1x_device, dev);
405 int host1x_device_init(struct host1x_device *device);
406 int host1x_device_exit(struct host1x_device *device);
408 void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key);
409 void host1x_client_exit(struct host1x_client *client);
411 #define host1x_client_init(client) \
413 static struct lock_class_key __key; \
414 __host1x_client_init(client, &__key); \
417 int __host1x_client_register(struct host1x_client *client);
420 * Note that this wrapper calls __host1x_client_init() for compatibility
421 * with existing callers. Callers that want to separately initialize and
422 * register a host1x client must first initialize using either of the
423 * __host1x_client_init() or host1x_client_init() functions and then use
424 * the low-level __host1x_client_register() function to avoid the client
425 * getting reinitialized.
427 #define host1x_client_register(client) \
429 static struct lock_class_key __key; \
430 __host1x_client_init(client, &__key); \
431 __host1x_client_register(client); \
434 int host1x_client_unregister(struct host1x_client *client);
436 int host1x_client_suspend(struct host1x_client *client);
437 int host1x_client_resume(struct host1x_client *client);
439 struct tegra_mipi_device;
441 struct tegra_mipi_device *tegra_mipi_request(struct device *device,
442 struct device_node *np);
443 void tegra_mipi_free(struct tegra_mipi_device *device);
444 int tegra_mipi_enable(struct tegra_mipi_device *device);
445 int tegra_mipi_disable(struct tegra_mipi_device *device);
446 int tegra_mipi_start_calibration(struct tegra_mipi_device *device);
447 int tegra_mipi_finish_calibration(struct tegra_mipi_device *device);