-
Notifications
You must be signed in to change notification settings - Fork 4
/
crb.c
307 lines (249 loc) · 5.64 KB
/
crb.c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2019 Apertus Solutions, LLC
*
* Author(s):
* Daniel P. Smith <dpsmith@apertussolutions.com>
*/
#ifdef LINUX_KERNEL
#include <linux/types.h>
#endif
#include "tpm.h"
#include "tpmbuff.h"
#include "crb.h"
#include "tpm_common.h"
#define TPM_LOC_STATE 0x0000
#define TPM_LOC_CTRL 0x0008
#define TPM_LOC_STS 0x000C
#define TPM_CRB_INTF_ID 0x0030
#define TPM_CRB_CTRL_EXT 0x0038
#define TPM_CRB_CTRL_REQ 0x0040
#define TPM_CRB_CTRL_STS 0x0044
#define TPM_CRB_CTRL_CANCEL 0x0048
#define TPM_CRB_CTRL_START 0x004C
#define TPM_CRB_INT_ENABLE 0x0050
#define TPM_CRB_INT_STS 0x0054
#define TPM_CRB_CTRL_CMD_SIZE 0x0058
#define TPM_CRB_CTRL_CMD_LADDR 0x005C
#define TPM_CRB_CTRL_CMD_HADDR 0x0060
#define TPM_CRB_CTRL_RSP_SIZE 0x0064
#define TPM_CRB_CTRL_RSP_ADDR 0x0068
#define TPM_CRB_DATA_BUFFER 0x0080
#define REGISTER(l, r) (((l) << 12) | (r))
static u8 locality = TPM_NO_LOCALITY;
struct tpm_loc_state {
union {
u8 val;
struct {
u8 tpm_established:1;
u8 loc_assigned:1;
u8 active_locality:3;
u8 _reserved:2;
u8 tpm_reg_valid_sts:1;
};
};
} __packed;
struct tpm_loc_ctrl {
union {
u32 val;
struct {
u32 request_access:1;
u32 relinquish:1;
u32 seize:1;
u32 reset_establishment_bit:1;
u32 _reserved:28;
};
};
} __packed;
struct tpm_loc_sts {
union {
u32 val;
struct {
u32 granted:1;
u32 beenSeized:1;
u32 _reserved:30;
};
};
} __packed;
struct tpm_crb_ctrl_req {
union {
u32 val;
struct {
u32 cmd_ready:1;
u32 go_idle:1;
u32 _reserved:30;
};
};
} __packed;
struct tpm_crb_ctrl_sts {
union {
u32 val;
struct {
u32 tpm_sts:1;
u32 tpm_idle:1;
u32 _reserved:30;
};
};
} __packed;
struct tpm_crb_intf_id_ext {
union {
u32 val;
struct {
u32 vid:16;
u32 did:16;
};
};
} __packed;
/*
* Durations derived from Table 15 of the PTP but is purely an artifact of this
* implementation
*/
/* TPM Duration A: 20ms */
static void duration_a(void)
{
tpm_mdelay(20);
}
/* TPM Duration B: 750ms */
static void __maybe_unused duration_b(void)
{
tpm_mdelay(750);
}
/* TPM Duration C: 1000ms */
static void __maybe_unused duration_c(void)
{
tpm_mdelay(1000);
}
static u8 is_idle(void)
{
struct tpm_crb_ctrl_sts ctl_sts;
ctl_sts.val = tpm_read32(REGISTER(locality, TPM_CRB_CTRL_STS));
if (ctl_sts.tpm_idle == 1)
return 1;
return 0;
}
static u8 __maybe_unused is_ready(void)
{
struct tpm_crb_ctrl_sts ctl_sts;
ctl_sts.val = tpm_read32(REGISTER(locality, TPM_CRB_CTRL_STS));
return ctl_sts.val == 0;
}
static u8 is_cmd_exec(void)
{
u32 ctrl_start;
ctrl_start = tpm_read32(REGISTER(locality, TPM_CRB_CTRL_START));
if (ctrl_start == 1)
return 1;
return 0;
}
static s8 cmd_ready(void)
{
struct tpm_crb_ctrl_req ctl_req;
if (is_idle()) {
ctl_req.cmd_ready = 1;
tpm_write32(ctl_req.val, REGISTER(locality, TPM_CRB_CTRL_REQ));
tpm2_timeout_c();
if (is_idle())
return -1;
}
return 0;
}
static void go_idle(void)
{
struct tpm_crb_ctrl_req ctl_req;
if (is_idle())
return;
ctl_req.go_idle = 1;
tpm_write32(ctl_req.val, REGISTER(locality, TPM_CRB_CTRL_REQ));
/* pause to give tpm time to complete the request */
tpm2_timeout_c();
}
static void crb_relinquish_locality_internal(u16 l)
{
struct tpm_loc_ctrl loc_ctrl;
loc_ctrl.relinquish = 1;
tpm_write32(loc_ctrl.val, REGISTER(l, TPM_LOC_CTRL));
}
u8 crb_request_locality(u8 l)
{
struct tpm_loc_state loc_state;
struct tpm_loc_ctrl loc_ctrl;
struct tpm_loc_sts loc_sts;
/* TPM_LOC_STATE is aliased across all localities */
loc_state.val = tpm_read8(REGISTER(0, TPM_LOC_STATE));
if (loc_state.loc_assigned == 1) {
if (loc_state.active_locality == l) {
locality = l;
return locality;
}
crb_relinquish_locality_internal(loc_state.loc_assigned);
}
loc_ctrl.request_access = 1;
tpm_write32(loc_ctrl.val, REGISTER(l, TPM_LOC_CTRL));
loc_sts.val = tpm_read32(REGISTER(l, TPM_LOC_STS));
if (loc_sts.granted != 1) {
locality = TPM_NO_LOCALITY;
return locality;
}
locality = l;
return locality;
}
void crb_relinquish_locality(void)
{
crb_relinquish_locality_internal(locality);
}
/* assumes cancel will succeed */
static void cancel_send(void)
{
if (is_cmd_exec()) {
tpm_write32(1, REGISTER(locality, TPM_CRB_CTRL_CANCEL));
timeout_b();
tpm_write32(0, REGISTER(locality, TPM_CRB_CTRL_CANCEL));
}
}
size_t crb_send(struct tpmbuff *buf)
{
u32 ctrl_start = 1;
if (is_idle())
return 0;
tpm_write32(ctrl_start, REGISTER(locality, TPM_CRB_CTRL_START));
/*
* Most command sequences this code is interested with operates with
* 20/750 duration/timeout schedule
*/
duration_a();
ctrl_start = tpm_read32(REGISTER(locality, TPM_CRB_CTRL_START));
if (ctrl_start != 0) {
timeout_a();
ctrl_start = tpm_read32(REGISTER(locality, TPM_CRB_CTRL_START));
if (ctrl_start != 0) {
cancel_send();
/* minimum response is header with cancel ord */
return sizeof(struct tpm_header);
}
}
return buf->len;
}
size_t crb_recv(__attribute__((unused)) enum tpm_family family,
__attribute__((unused)) struct tpmbuff *buf)
{
/* noop, currently send waits until execution is complete*/
return 0;
}
u8 crb_init(struct tpm *t)
{
struct tpm_crb_intf_id_ext id;
if (crb_request_locality(0) == TPM_NO_LOCALITY)
return 0;
id.val = tpm_read32(REGISTER(0, TPM_CRB_INTF_ID + 4));
t->vendor = ((id.vid & 0x00FF) << 8) | ((id.vid & 0xFF00) >> 8);
if ((t->vendor & 0xFFFF) == 0xFFFF)
return 0;
/* have the tpm invalidate the buffer if left in completion state */
go_idle();
/* now move to ready state */
cmd_ready();
t->ops.request_locality = crb_request_locality;
t->ops.relinquish_locality = crb_relinquish_locality;
t->ops.send = crb_send;
t->ops.recv = crb_recv;
return 1;
}