xref: /OK3568_Linux_fs/kernel/drivers/gpu/host1x/hw/syncpt_hw.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Tegra host1x Syncpoints
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2010-2013, NVIDIA Corporation.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/io.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "../dev.h"
11*4882a593Smuzhiyun #include "../syncpt.h"
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * Write the current syncpoint value back to hw.
15*4882a593Smuzhiyun  */
syncpt_restore(struct host1x_syncpt * sp)16*4882a593Smuzhiyun static void syncpt_restore(struct host1x_syncpt *sp)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	u32 min = host1x_syncpt_read_min(sp);
19*4882a593Smuzhiyun 	struct host1x *host = sp->host;
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 	host1x_sync_writel(host, min, HOST1X_SYNC_SYNCPT(sp->id));
22*4882a593Smuzhiyun }
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Write the current waitbase value back to hw.
26*4882a593Smuzhiyun  */
syncpt_restore_wait_base(struct host1x_syncpt * sp)27*4882a593Smuzhiyun static void syncpt_restore_wait_base(struct host1x_syncpt *sp)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun #if HOST1X_HW < 7
30*4882a593Smuzhiyun 	struct host1x *host = sp->host;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	host1x_sync_writel(host, sp->base_val,
33*4882a593Smuzhiyun 			   HOST1X_SYNC_SYNCPT_BASE(sp->id));
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * Read waitbase value from hw.
39*4882a593Smuzhiyun  */
syncpt_read_wait_base(struct host1x_syncpt * sp)40*4882a593Smuzhiyun static void syncpt_read_wait_base(struct host1x_syncpt *sp)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun #if HOST1X_HW < 7
43*4882a593Smuzhiyun 	struct host1x *host = sp->host;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	sp->base_val =
46*4882a593Smuzhiyun 		host1x_sync_readl(host, HOST1X_SYNC_SYNCPT_BASE(sp->id));
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * Updates the last value read from hardware.
52*4882a593Smuzhiyun  */
syncpt_load(struct host1x_syncpt * sp)53*4882a593Smuzhiyun static u32 syncpt_load(struct host1x_syncpt *sp)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	struct host1x *host = sp->host;
56*4882a593Smuzhiyun 	u32 old, live;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* Loop in case there's a race writing to min_val */
59*4882a593Smuzhiyun 	do {
60*4882a593Smuzhiyun 		old = host1x_syncpt_read_min(sp);
61*4882a593Smuzhiyun 		live = host1x_sync_readl(host, HOST1X_SYNC_SYNCPT(sp->id));
62*4882a593Smuzhiyun 	} while ((u32)atomic_cmpxchg(&sp->min_val, old, live) != old);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	if (!host1x_syncpt_check_max(sp, live))
65*4882a593Smuzhiyun 		dev_err(host->dev, "%s failed: id=%u, min=%d, max=%d\n",
66*4882a593Smuzhiyun 			__func__, sp->id, host1x_syncpt_read_min(sp),
67*4882a593Smuzhiyun 			host1x_syncpt_read_max(sp));
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return live;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun  * Write a cpu syncpoint increment to the hardware, without touching
74*4882a593Smuzhiyun  * the cache.
75*4882a593Smuzhiyun  */
syncpt_cpu_incr(struct host1x_syncpt * sp)76*4882a593Smuzhiyun static int syncpt_cpu_incr(struct host1x_syncpt *sp)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct host1x *host = sp->host;
79*4882a593Smuzhiyun 	u32 reg_offset = sp->id / 32;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if (!host1x_syncpt_client_managed(sp) &&
82*4882a593Smuzhiyun 	    host1x_syncpt_idle(sp))
83*4882a593Smuzhiyun 		return -EINVAL;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	host1x_sync_writel(host, BIT(sp->id % 32),
86*4882a593Smuzhiyun 			   HOST1X_SYNC_SYNCPT_CPU_INCR(reg_offset));
87*4882a593Smuzhiyun 	wmb();
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * syncpt_assign_to_channel() - Assign syncpoint to channel
94*4882a593Smuzhiyun  * @sp: syncpoint
95*4882a593Smuzhiyun  * @ch: channel
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * On chips with the syncpoint protection feature (Tegra186+), assign @sp to
98*4882a593Smuzhiyun  * @ch, preventing other channels from incrementing the syncpoints. If @ch is
99*4882a593Smuzhiyun  * NULL, unassigns the syncpoint.
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * On older chips, do nothing.
102*4882a593Smuzhiyun  */
syncpt_assign_to_channel(struct host1x_syncpt * sp,struct host1x_channel * ch)103*4882a593Smuzhiyun static void syncpt_assign_to_channel(struct host1x_syncpt *sp,
104*4882a593Smuzhiyun 				  struct host1x_channel *ch)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun #if HOST1X_HW >= 6
107*4882a593Smuzhiyun 	struct host1x *host = sp->host;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	if (!host->hv_regs)
110*4882a593Smuzhiyun 		return;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	host1x_sync_writel(host,
113*4882a593Smuzhiyun 			   HOST1X_SYNC_SYNCPT_CH_APP_CH(ch ? ch->id : 0xff),
114*4882a593Smuzhiyun 			   HOST1X_SYNC_SYNCPT_CH_APP(sp->id));
115*4882a593Smuzhiyun #endif
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun  * syncpt_enable_protection() - Enable syncpoint protection
120*4882a593Smuzhiyun  * @host: host1x instance
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * On chips with the syncpoint protection feature (Tegra186+), enable this
123*4882a593Smuzhiyun  * feature. On older chips, do nothing.
124*4882a593Smuzhiyun  */
syncpt_enable_protection(struct host1x * host)125*4882a593Smuzhiyun static void syncpt_enable_protection(struct host1x *host)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun #if HOST1X_HW >= 6
128*4882a593Smuzhiyun 	if (!host->hv_regs)
129*4882a593Smuzhiyun 		return;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	host1x_hypervisor_writel(host, HOST1X_HV_SYNCPT_PROT_EN_CH_EN,
132*4882a593Smuzhiyun 				 HOST1X_HV_SYNCPT_PROT_EN);
133*4882a593Smuzhiyun #endif
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun static const struct host1x_syncpt_ops host1x_syncpt_ops = {
137*4882a593Smuzhiyun 	.restore = syncpt_restore,
138*4882a593Smuzhiyun 	.restore_wait_base = syncpt_restore_wait_base,
139*4882a593Smuzhiyun 	.load_wait_base = syncpt_read_wait_base,
140*4882a593Smuzhiyun 	.load = syncpt_load,
141*4882a593Smuzhiyun 	.cpu_incr = syncpt_cpu_incr,
142*4882a593Smuzhiyun 	.assign_to_channel = syncpt_assign_to_channel,
143*4882a593Smuzhiyun 	.enable_protection = syncpt_enable_protection,
144*4882a593Smuzhiyun };
145