1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (c) 2012-2015, NVIDIA Corporation.
4 */
5
6#ifndef HOST1X_DEV_H
7#define HOST1X_DEV_H
8
9#include <linux/device.h>
10#include <linux/iommu.h>
11#include <linux/iova.h>
12#include <linux/platform_device.h>
13#include <linux/reset.h>
14
15#include "cdma.h"
16#include "channel.h"
17#include "context.h"
18#include "intr.h"
19#include "job.h"
20#include "syncpt.h"
21
22struct host1x_syncpt;
23struct host1x_syncpt_base;
24struct host1x_channel;
25struct host1x_cdma;
26struct host1x_job;
27struct push_buffer;
28struct output;
29struct dentry;
30
31struct host1x_channel_ops {
32	int (*init)(struct host1x_channel *channel, struct host1x *host,
33		    unsigned int id);
34	int (*submit)(struct host1x_job *job);
35};
36
37struct host1x_cdma_ops {
38	void (*start)(struct host1x_cdma *cdma);
39	void (*stop)(struct host1x_cdma *cdma);
40	void (*flush)(struct  host1x_cdma *cdma);
41	int (*timeout_init)(struct host1x_cdma *cdma);
42	void (*timeout_destroy)(struct host1x_cdma *cdma);
43	void (*freeze)(struct host1x_cdma *cdma);
44	void (*resume)(struct host1x_cdma *cdma, u32 getptr);
45	void (*timeout_cpu_incr)(struct host1x_cdma *cdma, u32 getptr,
46				 u32 syncpt_incrs, u32 syncval, u32 nr_slots);
47};
48
49struct host1x_pushbuffer_ops {
50	void (*init)(struct push_buffer *pb);
51};
52
53struct host1x_debug_ops {
54	void (*debug_init)(struct dentry *de);
55	void (*show_channel_cdma)(struct host1x *host,
56				  struct host1x_channel *ch,
57				  struct output *o);
58	void (*show_channel_fifo)(struct host1x *host,
59				  struct host1x_channel *ch,
60				  struct output *o);
61	void (*show_mlocks)(struct host1x *host, struct output *output);
62
63};
64
65struct host1x_syncpt_ops {
66	void (*restore)(struct host1x_syncpt *syncpt);
67	void (*restore_wait_base)(struct host1x_syncpt *syncpt);
68	void (*load_wait_base)(struct host1x_syncpt *syncpt);
69	u32 (*load)(struct host1x_syncpt *syncpt);
70	int (*cpu_incr)(struct host1x_syncpt *syncpt);
71	void (*assign_to_channel)(struct host1x_syncpt *syncpt,
72	                          struct host1x_channel *channel);
73	void (*enable_protection)(struct host1x *host);
74};
75
76struct host1x_intr_ops {
77	int (*init_host_sync)(struct host1x *host, u32 cpm);
78	void (*set_syncpt_threshold)(
79		struct host1x *host, unsigned int id, u32 thresh);
80	void (*enable_syncpt_intr)(struct host1x *host, unsigned int id);
81	void (*disable_syncpt_intr)(struct host1x *host, unsigned int id);
82	void (*disable_all_syncpt_intrs)(struct host1x *host);
83	int (*free_syncpt_irq)(struct host1x *host);
84};
85
86struct host1x_sid_entry {
87	unsigned int base;
88	unsigned int offset;
89	unsigned int limit;
90};
91
92struct host1x_table_desc {
93	unsigned int base;
94	unsigned int count;
95};
96
97struct host1x_info {
98	unsigned int nb_channels; /* host1x: number of channels supported */
99	unsigned int nb_pts; /* host1x: number of syncpoints supported */
100	unsigned int nb_bases; /* host1x: number of syncpoint bases supported */
101	unsigned int nb_mlocks; /* host1x: number of mlocks supported */
102	int (*init)(struct host1x *host1x); /* initialize per SoC ops */
103	unsigned int sync_offset; /* offset of syncpoint registers */
104	u64 dma_mask; /* mask of addressable memory */
105	bool has_wide_gather; /* supports GATHER_W opcode */
106	bool has_hypervisor; /* has hypervisor registers */
107	bool has_common; /* has common registers separate from hypervisor */
108	unsigned int num_sid_entries;
109	const struct host1x_sid_entry *sid_table;
110	struct host1x_table_desc streamid_vm_table;
111	struct host1x_table_desc classid_vm_table;
112	struct host1x_table_desc mmio_vm_table;
113	/*
114	 * On T20-T148, the boot chain may setup DC to increment syncpoints
115	 * 26/27 on VBLANK. As such we cannot use these syncpoints until
116	 * the display driver disables VBLANK increments.
117	 */
118	bool reserve_vblank_syncpts;
119	/*
120	 * On Tegra186, secure world applications may require access to
121	 * host1x during suspend/resume. To allow this, we need to leave
122	 * host1x not in reset.
123	 */
124	bool skip_reset_assert;
125};
126
127struct host1x {
128	const struct host1x_info *info;
129
130	void __iomem *regs;
131	void __iomem *hv_regs; /* hypervisor region */
132	void __iomem *common_regs;
133	int syncpt_irqs[8];
134	int num_syncpt_irqs;
135	struct host1x_syncpt *syncpt;
136	struct host1x_syncpt_base *bases;
137	struct device *dev;
138	struct clk *clk;
139	struct reset_control_bulk_data resets[2];
140	unsigned int nresets;
141
142	struct iommu_group *group;
143	struct iommu_domain *domain;
144	struct iova_domain iova;
145	dma_addr_t iova_end;
146
147	struct mutex intr_mutex;
148
149	const struct host1x_syncpt_ops *syncpt_op;
150	const struct host1x_intr_ops *intr_op;
151	const struct host1x_channel_ops *channel_op;
152	const struct host1x_cdma_ops *cdma_op;
153	const struct host1x_pushbuffer_ops *cdma_pb_op;
154	const struct host1x_debug_ops *debug_op;
155
156	struct host1x_syncpt *nop_sp;
157
158	struct mutex syncpt_mutex;
159
160	struct host1x_channel_list channel_list;
161	struct host1x_memory_context_list context_list;
162
163	struct dentry *debugfs;
164
165	struct mutex devices_lock;
166	struct list_head devices;
167
168	struct list_head list;
169
170	struct device_dma_parameters dma_parms;
171
172	struct host1x_bo_cache cache;
173};
174
175void host1x_common_writel(struct host1x *host1x, u32 v, u32 r);
176void host1x_hypervisor_writel(struct host1x *host1x, u32 r, u32 v);
177u32 host1x_hypervisor_readl(struct host1x *host1x, u32 r);
178void host1x_sync_writel(struct host1x *host1x, u32 r, u32 v);
179u32 host1x_sync_readl(struct host1x *host1x, u32 r);
180void host1x_ch_writel(struct host1x_channel *ch, u32 r, u32 v);
181u32 host1x_ch_readl(struct host1x_channel *ch, u32 r);
182
183static inline void host1x_hw_syncpt_restore(struct host1x *host,
184					    struct host1x_syncpt *sp)
185{
186	host->syncpt_op->restore(sp);
187}
188
189static inline void host1x_hw_syncpt_restore_wait_base(struct host1x *host,
190						      struct host1x_syncpt *sp)
191{
192	host->syncpt_op->restore_wait_base(sp);
193}
194
195static inline void host1x_hw_syncpt_load_wait_base(struct host1x *host,
196						   struct host1x_syncpt *sp)
197{
198	host->syncpt_op->load_wait_base(sp);
199}
200
201static inline u32 host1x_hw_syncpt_load(struct host1x *host,
202					struct host1x_syncpt *sp)
203{
204	return host->syncpt_op->load(sp);
205}
206
207static inline int host1x_hw_syncpt_cpu_incr(struct host1x *host,
208					    struct host1x_syncpt *sp)
209{
210	return host->syncpt_op->cpu_incr(sp);
211}
212
213static inline void host1x_hw_syncpt_assign_to_channel(
214	struct host1x *host, struct host1x_syncpt *sp,
215	struct host1x_channel *ch)
216{
217	return host->syncpt_op->assign_to_channel(sp, ch);
218}
219
220static inline void host1x_hw_syncpt_enable_protection(struct host1x *host)
221{
222	return host->syncpt_op->enable_protection(host);
223}
224
225static inline int host1x_hw_intr_init_host_sync(struct host1x *host, u32 cpm)
226{
227	return host->intr_op->init_host_sync(host, cpm);
228}
229
230static inline void host1x_hw_intr_set_syncpt_threshold(struct host1x *host,
231						       unsigned int id,
232						       u32 thresh)
233{
234	host->intr_op->set_syncpt_threshold(host, id, thresh);
235}
236
237static inline void host1x_hw_intr_enable_syncpt_intr(struct host1x *host,
238						     unsigned int id)
239{
240	host->intr_op->enable_syncpt_intr(host, id);
241}
242
243static inline void host1x_hw_intr_disable_syncpt_intr(struct host1x *host,
244						      unsigned int id)
245{
246	host->intr_op->disable_syncpt_intr(host, id);
247}
248
249static inline void host1x_hw_intr_disable_all_syncpt_intrs(struct host1x *host)
250{
251	host->intr_op->disable_all_syncpt_intrs(host);
252}
253
254static inline int host1x_hw_intr_free_syncpt_irq(struct host1x *host)
255{
256	return host->intr_op->free_syncpt_irq(host);
257}
258
259static inline int host1x_hw_channel_init(struct host1x *host,
260					 struct host1x_channel *channel,
261					 unsigned int id)
262{
263	return host->channel_op->init(channel, host, id);
264}
265
266static inline int host1x_hw_channel_submit(struct host1x *host,
267					   struct host1x_job *job)
268{
269	return host->channel_op->submit(job);
270}
271
272static inline void host1x_hw_cdma_start(struct host1x *host,
273					struct host1x_cdma *cdma)
274{
275	host->cdma_op->start(cdma);
276}
277
278static inline void host1x_hw_cdma_stop(struct host1x *host,
279				       struct host1x_cdma *cdma)
280{
281	host->cdma_op->stop(cdma);
282}
283
284static inline void host1x_hw_cdma_flush(struct host1x *host,
285					struct host1x_cdma *cdma)
286{
287	host->cdma_op->flush(cdma);
288}
289
290static inline int host1x_hw_cdma_timeout_init(struct host1x *host,
291					      struct host1x_cdma *cdma)
292{
293	return host->cdma_op->timeout_init(cdma);
294}
295
296static inline void host1x_hw_cdma_timeout_destroy(struct host1x *host,
297						  struct host1x_cdma *cdma)
298{
299	host->cdma_op->timeout_destroy(cdma);
300}
301
302static inline void host1x_hw_cdma_freeze(struct host1x *host,
303					 struct host1x_cdma *cdma)
304{
305	host->cdma_op->freeze(cdma);
306}
307
308static inline void host1x_hw_cdma_resume(struct host1x *host,
309					 struct host1x_cdma *cdma, u32 getptr)
310{
311	host->cdma_op->resume(cdma, getptr);
312}
313
314static inline void host1x_hw_cdma_timeout_cpu_incr(struct host1x *host,
315						   struct host1x_cdma *cdma,
316						   u32 getptr,
317						   u32 syncpt_incrs,
318						   u32 syncval, u32 nr_slots)
319{
320	host->cdma_op->timeout_cpu_incr(cdma, getptr, syncpt_incrs, syncval,
321					nr_slots);
322}
323
324static inline void host1x_hw_pushbuffer_init(struct host1x *host,
325					     struct push_buffer *pb)
326{
327	host->cdma_pb_op->init(pb);
328}
329
330static inline void host1x_hw_debug_init(struct host1x *host, struct dentry *de)
331{
332	if (host->debug_op && host->debug_op->debug_init)
333		host->debug_op->debug_init(de);
334}
335
336static inline void host1x_hw_show_channel_cdma(struct host1x *host,
337					       struct host1x_channel *channel,
338					       struct output *o)
339{
340	host->debug_op->show_channel_cdma(host, channel, o);
341}
342
343static inline void host1x_hw_show_channel_fifo(struct host1x *host,
344					       struct host1x_channel *channel,
345					       struct output *o)
346{
347	host->debug_op->show_channel_fifo(host, channel, o);
348}
349
350static inline void host1x_hw_show_mlocks(struct host1x *host, struct output *o)
351{
352	host->debug_op->show_mlocks(host, o);
353}
354
355extern struct platform_driver tegra_mipi_driver;
356
357#endif
358