1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* Copyright (c) 2013-2017, NVIDIA CORPORATION. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/io.h>
#include <linux/of_address.h>
#include <soc/tegra/doorbell.h>
#include "bpmp.h"
static void __iomem *arb_sema;
#define CPU_OB_DOORBELL 4
#define TRIGGER_OFFSET 0x000
#define RESULT_OFFSET(id) (0xc00 + id * 4)
#define TRIGGER_ID_SHIFT 16
#define TRIGGER_CMD_GET 4
#define STA_OFFSET 0
#define SET_OFFSET 4
#define CLR_OFFSET 8
/*
* How the token bits are interpretted
*
* SL_SIGL (b00): slave ch in signalled state
* SL_QUED (b01): slave ch is in queue
* MA_FREE (b10): master ch is free
* MA_ACKD (b11): master ch is acked
*
* Ideally, the slave should only set bits while the
* master do only clear them. But there is an exception -
* see bpmp_ack_master()
*/
#define CH_MASK(ch) (0x3 << ((ch) * 2))
#define SL_SIGL(ch) (0x0 << ((ch) * 2))
#define SL_QUED(ch) (0x1 << ((ch) * 2))
#define MA_FREE(ch) (0x2 << ((ch) * 2))
#define MA_ACKD(ch) (0x3 << ((ch) * 2))
static u32 bpmp_ch_sta(int ch)
{
return __raw_readl(arb_sema + STA_OFFSET) & CH_MASK(ch);
}
static bool bpmp_master_free(const struct mail_ops *ops, int ch)
{
return bpmp_ch_sta(ch) == MA_FREE(ch);
}
static bool bpmp_slave_signalled(const struct mail_ops *ops, int ch)
{
return bpmp_ch_sta(ch) == SL_SIGL(ch);
}
static bool bpmp_master_acked(const struct mail_ops *ops, int ch)
{
return bpmp_ch_sta(ch) == MA_ACKD(ch);
}
static void bpmp_signal_slave(const struct mail_ops *ops, int ch)
{
__raw_writel(CH_MASK(ch), arb_sema + CLR_OFFSET);
}
static void bpmp_ack_master(int ch, int flags)
{
__raw_writel(MA_ACKD(ch), arb_sema + SET_OFFSET);
if (flags & DO_ACK)
return;
/*
* We have to violate the bit modification rule while
* moving from SL_QUED to MA_FREE (DO_ACK not set) so that
* the channel won't be in ACKD state forever.
*/
__raw_writel(MA_ACKD(ch) ^ MA_FREE(ch), arb_sema + CLR_OFFSET);
}
/* MA_ACKD to MA_FREE */
static void bpmp_free_master(const struct mail_ops *ops, int ch)
{
__raw_writel(MA_ACKD(ch) ^ MA_FREE(ch), arb_sema + CLR_OFFSET);
}
static void bpmp_ring_doorbell(int ch)
{
tegra_ring_doorbell(CPU_OB_DOORBELL);
}
static void bpmp_return_data(const struct mail_ops *ops,
int ch, int code, void *data, int sz)
{
struct mb_data *p;
int flags;
if (sz > MSG_DATA_MIN_SZ) {
WARN_ON(1);
return;
}
p = channel_area[ch].ob;
p->code = code;
memcpy(p->data, data, sz);
flags = channel_area[ch].ib->flags;
bpmp_ack_master(ch, flags);
if (flags & RING_DOORBELL)
bpmp_ring_doorbell(ch);
}
static void bpmp_doorbell_handler(void *data)
{
unsigned int chidx = (long)data;
bpmp_handle_irq(chidx);
}
static int bpmp_init_irq(unsigned int cnt)
{
unsigned int i;
long chidx;
int r;
for (i = 0; i < cnt; i++) {
chidx = i;
r = tegra_register_doorbell_handler(i, bpmp_doorbell_handler,
(void *)chidx);
if (r)
return r;
}
return 0;
}
/* Channel area is setup by BPMP before signalling handshake */
static u32 bpmp_channel_area(void __iomem *atomics, int ch)
{
u32 a;
writel(ch << TRIGGER_ID_SHIFT | TRIGGER_CMD_GET,
atomics + TRIGGER_OFFSET);
a = readl(atomics + RESULT_OFFSET(ch));
return a;
}
static int bpmp_connect(const struct channel_cfg *cfg,
const struct mail_ops *ops, struct device_node *of_node)
{
unsigned int chmask;
void __iomem *atomics;
uint32_t area;
void *p;
int i;
atomics = of_iomap(of_node, 0);
if (!atomics)
return -ENODEV;
arb_sema = of_iomap(of_node, 1);
if (!arb_sema)
return -ENODEV;
/* handshake */
if (!readl(arb_sema + STA_OFFSET))
return -ENODEV;
chmask = cfg->channel_mask;
while (chmask) {
i = __ffs(chmask);
chmask &= ~(1 << i);
area = bpmp_channel_area(atomics, i);
if (!area)
return -EFAULT;
p = ioremap(area, 0x80);
if (!p)
return -ENOMEM;
channel_area[i].ib = p;
channel_area[i].ob = p;
}
return 0;
}
const struct mail_ops t210_mail_ops = {
.init_irq = bpmp_init_irq,
.connect = bpmp_connect,
.master_free = bpmp_master_free,
.free_master = bpmp_free_master,
.master_acked = bpmp_master_acked,
.signal_slave = bpmp_signal_slave,
.ring_doorbell = bpmp_ring_doorbell,
.slave_signalled = bpmp_slave_signalled,
.return_data = bpmp_return_data
};
|