162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2012-2015, NVIDIA Corporation.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef HOST1X_DEV_H
762306a36Sopenharmony_ci#define HOST1X_DEV_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/device.h>
1062306a36Sopenharmony_ci#include <linux/iommu.h>
1162306a36Sopenharmony_ci#include <linux/iova.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci#include <linux/reset.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "cdma.h"
1662306a36Sopenharmony_ci#include "channel.h"
1762306a36Sopenharmony_ci#include "context.h"
1862306a36Sopenharmony_ci#include "intr.h"
1962306a36Sopenharmony_ci#include "job.h"
2062306a36Sopenharmony_ci#include "syncpt.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct host1x_syncpt;
2362306a36Sopenharmony_cistruct host1x_syncpt_base;
2462306a36Sopenharmony_cistruct host1x_channel;
2562306a36Sopenharmony_cistruct host1x_cdma;
2662306a36Sopenharmony_cistruct host1x_job;
2762306a36Sopenharmony_cistruct push_buffer;
2862306a36Sopenharmony_cistruct output;
2962306a36Sopenharmony_cistruct dentry;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistruct host1x_channel_ops {
3262306a36Sopenharmony_ci	int (*init)(struct host1x_channel *channel, struct host1x *host,
3362306a36Sopenharmony_ci		    unsigned int id);
3462306a36Sopenharmony_ci	int (*submit)(struct host1x_job *job);
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct host1x_cdma_ops {
3862306a36Sopenharmony_ci	void (*start)(struct host1x_cdma *cdma);
3962306a36Sopenharmony_ci	void (*stop)(struct host1x_cdma *cdma);
4062306a36Sopenharmony_ci	void (*flush)(struct  host1x_cdma *cdma);
4162306a36Sopenharmony_ci	int (*timeout_init)(struct host1x_cdma *cdma);
4262306a36Sopenharmony_ci	void (*timeout_destroy)(struct host1x_cdma *cdma);
4362306a36Sopenharmony_ci	void (*freeze)(struct host1x_cdma *cdma);
4462306a36Sopenharmony_ci	void (*resume)(struct host1x_cdma *cdma, u32 getptr);
4562306a36Sopenharmony_ci	void (*timeout_cpu_incr)(struct host1x_cdma *cdma, u32 getptr,
4662306a36Sopenharmony_ci				 u32 syncpt_incrs, u32 syncval, u32 nr_slots);
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistruct host1x_pushbuffer_ops {
5062306a36Sopenharmony_ci	void (*init)(struct push_buffer *pb);
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistruct host1x_debug_ops {
5462306a36Sopenharmony_ci	void (*debug_init)(struct dentry *de);
5562306a36Sopenharmony_ci	void (*show_channel_cdma)(struct host1x *host,
5662306a36Sopenharmony_ci				  struct host1x_channel *ch,
5762306a36Sopenharmony_ci				  struct output *o);
5862306a36Sopenharmony_ci	void (*show_channel_fifo)(struct host1x *host,
5962306a36Sopenharmony_ci				  struct host1x_channel *ch,
6062306a36Sopenharmony_ci				  struct output *o);
6162306a36Sopenharmony_ci	void (*show_mlocks)(struct host1x *host, struct output *output);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci};
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistruct host1x_syncpt_ops {
6662306a36Sopenharmony_ci	void (*restore)(struct host1x_syncpt *syncpt);
6762306a36Sopenharmony_ci	void (*restore_wait_base)(struct host1x_syncpt *syncpt);
6862306a36Sopenharmony_ci	void (*load_wait_base)(struct host1x_syncpt *syncpt);
6962306a36Sopenharmony_ci	u32 (*load)(struct host1x_syncpt *syncpt);
7062306a36Sopenharmony_ci	int (*cpu_incr)(struct host1x_syncpt *syncpt);
7162306a36Sopenharmony_ci	void (*assign_to_channel)(struct host1x_syncpt *syncpt,
7262306a36Sopenharmony_ci	                          struct host1x_channel *channel);
7362306a36Sopenharmony_ci	void (*enable_protection)(struct host1x *host);
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistruct host1x_intr_ops {
7762306a36Sopenharmony_ci	int (*init_host_sync)(struct host1x *host, u32 cpm);
7862306a36Sopenharmony_ci	void (*set_syncpt_threshold)(
7962306a36Sopenharmony_ci		struct host1x *host, unsigned int id, u32 thresh);
8062306a36Sopenharmony_ci	void (*enable_syncpt_intr)(struct host1x *host, unsigned int id);
8162306a36Sopenharmony_ci	void (*disable_syncpt_intr)(struct host1x *host, unsigned int id);
8262306a36Sopenharmony_ci	void (*disable_all_syncpt_intrs)(struct host1x *host);
8362306a36Sopenharmony_ci	int (*free_syncpt_irq)(struct host1x *host);
8462306a36Sopenharmony_ci};
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistruct host1x_sid_entry {
8762306a36Sopenharmony_ci	unsigned int base;
8862306a36Sopenharmony_ci	unsigned int offset;
8962306a36Sopenharmony_ci	unsigned int limit;
9062306a36Sopenharmony_ci};
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_cistruct host1x_table_desc {
9362306a36Sopenharmony_ci	unsigned int base;
9462306a36Sopenharmony_ci	unsigned int count;
9562306a36Sopenharmony_ci};
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistruct host1x_info {
9862306a36Sopenharmony_ci	unsigned int nb_channels; /* host1x: number of channels supported */
9962306a36Sopenharmony_ci	unsigned int nb_pts; /* host1x: number of syncpoints supported */
10062306a36Sopenharmony_ci	unsigned int nb_bases; /* host1x: number of syncpoint bases supported */
10162306a36Sopenharmony_ci	unsigned int nb_mlocks; /* host1x: number of mlocks supported */
10262306a36Sopenharmony_ci	int (*init)(struct host1x *host1x); /* initialize per SoC ops */
10362306a36Sopenharmony_ci	unsigned int sync_offset; /* offset of syncpoint registers */
10462306a36Sopenharmony_ci	u64 dma_mask; /* mask of addressable memory */
10562306a36Sopenharmony_ci	bool has_wide_gather; /* supports GATHER_W opcode */
10662306a36Sopenharmony_ci	bool has_hypervisor; /* has hypervisor registers */
10762306a36Sopenharmony_ci	bool has_common; /* has common registers separate from hypervisor */
10862306a36Sopenharmony_ci	unsigned int num_sid_entries;
10962306a36Sopenharmony_ci	const struct host1x_sid_entry *sid_table;
11062306a36Sopenharmony_ci	struct host1x_table_desc streamid_vm_table;
11162306a36Sopenharmony_ci	struct host1x_table_desc classid_vm_table;
11262306a36Sopenharmony_ci	struct host1x_table_desc mmio_vm_table;
11362306a36Sopenharmony_ci	/*
11462306a36Sopenharmony_ci	 * On T20-T148, the boot chain may setup DC to increment syncpoints
11562306a36Sopenharmony_ci	 * 26/27 on VBLANK. As such we cannot use these syncpoints until
11662306a36Sopenharmony_ci	 * the display driver disables VBLANK increments.
11762306a36Sopenharmony_ci	 */
11862306a36Sopenharmony_ci	bool reserve_vblank_syncpts;
11962306a36Sopenharmony_ci};
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistruct host1x {
12262306a36Sopenharmony_ci	const struct host1x_info *info;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	void __iomem *regs;
12562306a36Sopenharmony_ci	void __iomem *hv_regs; /* hypervisor region */
12662306a36Sopenharmony_ci	void __iomem *common_regs;
12762306a36Sopenharmony_ci	int syncpt_irq;
12862306a36Sopenharmony_ci	struct host1x_syncpt *syncpt;
12962306a36Sopenharmony_ci	struct host1x_syncpt_base *bases;
13062306a36Sopenharmony_ci	struct device *dev;
13162306a36Sopenharmony_ci	struct clk *clk;
13262306a36Sopenharmony_ci	struct reset_control_bulk_data resets[2];
13362306a36Sopenharmony_ci	unsigned int nresets;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	struct iommu_group *group;
13662306a36Sopenharmony_ci	struct iommu_domain *domain;
13762306a36Sopenharmony_ci	struct iova_domain iova;
13862306a36Sopenharmony_ci	dma_addr_t iova_end;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	struct mutex intr_mutex;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	const struct host1x_syncpt_ops *syncpt_op;
14362306a36Sopenharmony_ci	const struct host1x_intr_ops *intr_op;
14462306a36Sopenharmony_ci	const struct host1x_channel_ops *channel_op;
14562306a36Sopenharmony_ci	const struct host1x_cdma_ops *cdma_op;
14662306a36Sopenharmony_ci	const struct host1x_pushbuffer_ops *cdma_pb_op;
14762306a36Sopenharmony_ci	const struct host1x_debug_ops *debug_op;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	struct host1x_syncpt *nop_sp;
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	struct mutex syncpt_mutex;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	struct host1x_channel_list channel_list;
15462306a36Sopenharmony_ci	struct host1x_memory_context_list context_list;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	struct dentry *debugfs;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	struct mutex devices_lock;
15962306a36Sopenharmony_ci	struct list_head devices;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	struct list_head list;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	struct device_dma_parameters dma_parms;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	struct host1x_bo_cache cache;
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_civoid host1x_common_writel(struct host1x *host1x, u32 v, u32 r);
16962306a36Sopenharmony_civoid host1x_hypervisor_writel(struct host1x *host1x, u32 r, u32 v);
17062306a36Sopenharmony_ciu32 host1x_hypervisor_readl(struct host1x *host1x, u32 r);
17162306a36Sopenharmony_civoid host1x_sync_writel(struct host1x *host1x, u32 r, u32 v);
17262306a36Sopenharmony_ciu32 host1x_sync_readl(struct host1x *host1x, u32 r);
17362306a36Sopenharmony_civoid host1x_ch_writel(struct host1x_channel *ch, u32 r, u32 v);
17462306a36Sopenharmony_ciu32 host1x_ch_readl(struct host1x_channel *ch, u32 r);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic inline void host1x_hw_syncpt_restore(struct host1x *host,
17762306a36Sopenharmony_ci					    struct host1x_syncpt *sp)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	host->syncpt_op->restore(sp);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic inline void host1x_hw_syncpt_restore_wait_base(struct host1x *host,
18362306a36Sopenharmony_ci						      struct host1x_syncpt *sp)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	host->syncpt_op->restore_wait_base(sp);
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_cistatic inline void host1x_hw_syncpt_load_wait_base(struct host1x *host,
18962306a36Sopenharmony_ci						   struct host1x_syncpt *sp)
19062306a36Sopenharmony_ci{
19162306a36Sopenharmony_ci	host->syncpt_op->load_wait_base(sp);
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cistatic inline u32 host1x_hw_syncpt_load(struct host1x *host,
19562306a36Sopenharmony_ci					struct host1x_syncpt *sp)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	return host->syncpt_op->load(sp);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic inline int host1x_hw_syncpt_cpu_incr(struct host1x *host,
20162306a36Sopenharmony_ci					    struct host1x_syncpt *sp)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	return host->syncpt_op->cpu_incr(sp);
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic inline void host1x_hw_syncpt_assign_to_channel(
20762306a36Sopenharmony_ci	struct host1x *host, struct host1x_syncpt *sp,
20862306a36Sopenharmony_ci	struct host1x_channel *ch)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	return host->syncpt_op->assign_to_channel(sp, ch);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic inline void host1x_hw_syncpt_enable_protection(struct host1x *host)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	return host->syncpt_op->enable_protection(host);
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic inline int host1x_hw_intr_init_host_sync(struct host1x *host, u32 cpm)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	return host->intr_op->init_host_sync(host, cpm);
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistatic inline void host1x_hw_intr_set_syncpt_threshold(struct host1x *host,
22462306a36Sopenharmony_ci						       unsigned int id,
22562306a36Sopenharmony_ci						       u32 thresh)
22662306a36Sopenharmony_ci{
22762306a36Sopenharmony_ci	host->intr_op->set_syncpt_threshold(host, id, thresh);
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cistatic inline void host1x_hw_intr_enable_syncpt_intr(struct host1x *host,
23162306a36Sopenharmony_ci						     unsigned int id)
23262306a36Sopenharmony_ci{
23362306a36Sopenharmony_ci	host->intr_op->enable_syncpt_intr(host, id);
23462306a36Sopenharmony_ci}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic inline void host1x_hw_intr_disable_syncpt_intr(struct host1x *host,
23762306a36Sopenharmony_ci						      unsigned int id)
23862306a36Sopenharmony_ci{
23962306a36Sopenharmony_ci	host->intr_op->disable_syncpt_intr(host, id);
24062306a36Sopenharmony_ci}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cistatic inline void host1x_hw_intr_disable_all_syncpt_intrs(struct host1x *host)
24362306a36Sopenharmony_ci{
24462306a36Sopenharmony_ci	host->intr_op->disable_all_syncpt_intrs(host);
24562306a36Sopenharmony_ci}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cistatic inline int host1x_hw_intr_free_syncpt_irq(struct host1x *host)
24862306a36Sopenharmony_ci{
24962306a36Sopenharmony_ci	return host->intr_op->free_syncpt_irq(host);
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_cistatic inline int host1x_hw_channel_init(struct host1x *host,
25362306a36Sopenharmony_ci					 struct host1x_channel *channel,
25462306a36Sopenharmony_ci					 unsigned int id)
25562306a36Sopenharmony_ci{
25662306a36Sopenharmony_ci	return host->channel_op->init(channel, host, id);
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic inline int host1x_hw_channel_submit(struct host1x *host,
26062306a36Sopenharmony_ci					   struct host1x_job *job)
26162306a36Sopenharmony_ci{
26262306a36Sopenharmony_ci	return host->channel_op->submit(job);
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_cistatic inline void host1x_hw_cdma_start(struct host1x *host,
26662306a36Sopenharmony_ci					struct host1x_cdma *cdma)
26762306a36Sopenharmony_ci{
26862306a36Sopenharmony_ci	host->cdma_op->start(cdma);
26962306a36Sopenharmony_ci}
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_cistatic inline void host1x_hw_cdma_stop(struct host1x *host,
27262306a36Sopenharmony_ci				       struct host1x_cdma *cdma)
27362306a36Sopenharmony_ci{
27462306a36Sopenharmony_ci	host->cdma_op->stop(cdma);
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_cistatic inline void host1x_hw_cdma_flush(struct host1x *host,
27862306a36Sopenharmony_ci					struct host1x_cdma *cdma)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	host->cdma_op->flush(cdma);
28162306a36Sopenharmony_ci}
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_cistatic inline int host1x_hw_cdma_timeout_init(struct host1x *host,
28462306a36Sopenharmony_ci					      struct host1x_cdma *cdma)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	return host->cdma_op->timeout_init(cdma);
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_cistatic inline void host1x_hw_cdma_timeout_destroy(struct host1x *host,
29062306a36Sopenharmony_ci						  struct host1x_cdma *cdma)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	host->cdma_op->timeout_destroy(cdma);
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic inline void host1x_hw_cdma_freeze(struct host1x *host,
29662306a36Sopenharmony_ci					 struct host1x_cdma *cdma)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	host->cdma_op->freeze(cdma);
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic inline void host1x_hw_cdma_resume(struct host1x *host,
30262306a36Sopenharmony_ci					 struct host1x_cdma *cdma, u32 getptr)
30362306a36Sopenharmony_ci{
30462306a36Sopenharmony_ci	host->cdma_op->resume(cdma, getptr);
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic inline void host1x_hw_cdma_timeout_cpu_incr(struct host1x *host,
30862306a36Sopenharmony_ci						   struct host1x_cdma *cdma,
30962306a36Sopenharmony_ci						   u32 getptr,
31062306a36Sopenharmony_ci						   u32 syncpt_incrs,
31162306a36Sopenharmony_ci						   u32 syncval, u32 nr_slots)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	host->cdma_op->timeout_cpu_incr(cdma, getptr, syncpt_incrs, syncval,
31462306a36Sopenharmony_ci					nr_slots);
31562306a36Sopenharmony_ci}
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cistatic inline void host1x_hw_pushbuffer_init(struct host1x *host,
31862306a36Sopenharmony_ci					     struct push_buffer *pb)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	host->cdma_pb_op->init(pb);
32162306a36Sopenharmony_ci}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_cistatic inline void host1x_hw_debug_init(struct host1x *host, struct dentry *de)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	if (host->debug_op && host->debug_op->debug_init)
32662306a36Sopenharmony_ci		host->debug_op->debug_init(de);
32762306a36Sopenharmony_ci}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cistatic inline void host1x_hw_show_channel_cdma(struct host1x *host,
33062306a36Sopenharmony_ci					       struct host1x_channel *channel,
33162306a36Sopenharmony_ci					       struct output *o)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	host->debug_op->show_channel_cdma(host, channel, o);
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_cistatic inline void host1x_hw_show_channel_fifo(struct host1x *host,
33762306a36Sopenharmony_ci					       struct host1x_channel *channel,
33862306a36Sopenharmony_ci					       struct output *o)
33962306a36Sopenharmony_ci{
34062306a36Sopenharmony_ci	host->debug_op->show_channel_fifo(host, channel, o);
34162306a36Sopenharmony_ci}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_cistatic inline void host1x_hw_show_mlocks(struct host1x *host, struct output *o)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	host->debug_op->show_mlocks(host, o);
34662306a36Sopenharmony_ci}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ciextern struct platform_driver tegra_mipi_driver;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci#endif
351