aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/tpm/tpm20.h
blob: 38340c33f76ac7011abe812814e632def277be42 (plain) (blame)
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
/*-
 * Copyright (c) 2018 Stormshield.
 * Copyright (c) 2018 Semihalf.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _TPM20_H_
#define _TPM20_H_

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/endian.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/conf.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/sx.h>
#include <sys/uio.h>

#include <machine/bus.h>
#include <machine/md_var.h>
#include <machine/resource.h>

#include <contrib/dev/acpica/include/acpi.h>
#include <contrib/dev/acpica/include/accommon.h>
#include <dev/acpica/acpivar.h>
#include "opt_acpi.h"

#define	BIT(x) (1 << (x))

/* Timeouts in us */
#define	TPM_TIMEOUT_A		750000
#define	TPM_TIMEOUT_B		2000000
#define	TPM_TIMEOUT_C		200000
#define	TPM_TIMEOUT_D		30000

/*
 * Generating RSA key pair takes ~(10-20s), which is significantly longer than
 * any timeout defined in spec. Because of that we need a new one.
 */
#define	TPM_TIMEOUT_LONG	40000000

/* List of commands that require TPM_TIMEOUT_LONG time to complete */
#define	TPM_CC_CreatePrimary	0x00000131
#define	TPM_CC_Create			0x00000153
#define	TPM_CC_CreateLoaded		0x00000191

/* List of commands that require only TPM_TIMEOUT_C time to complete */
#define TPM_CC_SequenceComplete			0x0000013e
#define TPM_CC_Startup					0x00000144
#define TPM_CC_SequenceUpdate			0x0000015c
#define TPM_CC_GetCapability			0x0000017a
#define TPM_CC_PCR_Extend				0x00000182
#define TPM_CC_EventSequenceComplete	0x00000185
#define TPM_CC_HashSequenceStart		0x00000186

/* Timeout before data in read buffer is discarded */
#define	TPM_READ_TIMEOUT	500000

#define	TPM_BUFSIZE		0x1000

#define	TPM_HEADER_SIZE		10

#define	TPM_CDEV_NAME		"tpm0"
#define	TPM_CDEV_PERM_FLAG	0600

struct tpm_sc {
	device_t	dev;

	struct resource	*mem_res;
	struct resource	*irq_res;
	int		mem_rid;
	int		irq_rid;

	struct cdev	*sc_cdev;

	struct sx 	dev_lock;
	struct cv	buf_cv;

	void 		*intr_cookie;
	int 		intr_type;	/* Current event type */
	bool 		interrupts;

	uint8_t 	*buf;
	size_t		pending_data_length;

	struct callout 	discard_buffer_callout;

	int		(*transmit)(struct tpm_sc *, size_t);
};

int tpm20_suspend(device_t dev);
int tpm20_shutdown(device_t dev);
int32_t tpm20_get_timeout(uint32_t command);
int tpm20_init(struct tpm_sc *sc);
void tpm20_release(struct tpm_sc *sc);

d_open_t	tpm20_open;
d_close_t	tpm20_close;
d_read_t	tpm20_read;
d_write_t	tpm20_write;
d_ioctl_t	tpm20_ioctl;

static struct cdevsw tpm_cdevsw = {
	.d_version = D_VERSION,
	.d_open = tpm20_open,
	.d_close = tpm20_close,
	.d_read = tpm20_read,
	.d_write = tpm20_write,
	.d_ioctl = tpm20_ioctl,
	.d_name = "tpm20",
};

/* Small helper routines for io ops */
static inline uint8_t
RD1(struct tpm_sc *sc, bus_size_t off)
{

	return (bus_read_1(sc->mem_res, off));
}
static inline uint32_t
RD4(struct tpm_sc *sc, bus_size_t off)
{

	return (bus_read_4(sc->mem_res, off));
}
static inline uint64_t
RD8(struct tpm_sc *sc, bus_size_t off)
{

	return (bus_read_8(sc->mem_res, off));
}
static inline void
WR1(struct tpm_sc *sc, bus_size_t off, uint8_t val)
{

	bus_write_1(sc->mem_res, off, val);
}
static inline void
WR4(struct tpm_sc *sc, bus_size_t off, uint32_t val)
{

	bus_write_4(sc->mem_res, off, val);
}
static inline void
AND4(struct tpm_sc *sc, bus_size_t off, uint32_t val)
{

	WR4(sc, off, RD4(sc, off) & val);
}
static inline void
OR1(struct tpm_sc *sc, bus_size_t off, uint8_t val)
{

	WR1(sc, off, RD1(sc, off) | val);
}
static inline void
OR4(struct tpm_sc *sc, bus_size_t off, uint32_t val)
{

	WR4(sc, off, RD4(sc, off) | val);
}
#endif	/* _TPM20_H_ */