xref: /freebsd/sys/dev/tpm/tpm20.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * Copyright (c) 2018 Stormshield.
3  * Copyright (c) 2018 Semihalf.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _TPM20_H_
29 #define	_TPM20_H_
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/endian.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 
41 #include <sys/bus.h>
42 #include <sys/callout.h>
43 #include <sys/conf.h>
44 #include <sys/module.h>
45 #include <sys/rman.h>
46 #include <sys/sx.h>
47 #include <sys/taskqueue.h>
48 #include <sys/uio.h>
49 
50 #include <machine/bus.h>
51 #include <machine/md_var.h>
52 #include <machine/resource.h>
53 
54 #include <contrib/dev/acpica/include/acpi.h>
55 #include <contrib/dev/acpica/include/accommon.h>
56 #include <dev/acpica/acpivar.h>
57 #include "opt_acpi.h"
58 
59 #include "opt_tpm.h"
60 
61 #define	BIT(x) (1 << (x))
62 
63 /* Timeouts in us */
64 #define	TPM_TIMEOUT_A			750000
65 #define	TPM_TIMEOUT_B			2000000
66 #define	TPM_TIMEOUT_C			200000
67 #define	TPM_TIMEOUT_D			30000
68 
69 /*
70  * Generating RSA key pair takes ~(10-20s), which is significantly longer than
71  * any timeout defined in spec. Because of that we need a new one.
72  */
73 #define	TPM_TIMEOUT_LONG		40000000
74 
75 /* List of commands that require TPM_TIMEOUT_LONG time to complete */
76 #define	TPM_CC_CreatePrimary		0x00000131
77 #define	TPM_CC_Create			0x00000153
78 #define	TPM_CC_CreateLoaded		0x00000191
79 
80 /* List of commands that require only TPM_TIMEOUT_C time to complete */
81 #define	TPM_CC_SequenceComplete		0x0000013e
82 #define	TPM_CC_Startup			0x00000144
83 #define	TPM_CC_SequenceUpdate		0x0000015c
84 #define	TPM_CC_GetCapability		0x0000017a
85 #define	TPM_CC_PCR_Extend		0x00000182
86 #define	TPM_CC_EventSequenceComplete	0x00000185
87 #define	TPM_CC_HashSequenceStart	0x00000186
88 
89 /* Timeout before data in read buffer is discarded */
90 #define	TPM_READ_TIMEOUT		500000
91 
92 #define	TPM_BUFSIZE			0x1000
93 
94 #define	TPM_HEADER_SIZE			10
95 
96 #define	TPM_CDEV_NAME			"tpm0"
97 #define	TPM_CDEV_PERM_FLAG		0600
98 
99 #define	TPM2_START_METHOD_ACPI		2
100 #define	TPM2_START_METHOD_TIS		6
101 #define	TPM2_START_METHOD_CRB		7
102 #define	TPM2_START_METHOD_CRB_ACPI	8
103 
104 MALLOC_DECLARE(M_TPM20);
105 
106 struct tpm_sc {
107 	device_t	dev;
108 
109 	struct resource	*mem_res;
110 	struct resource	*irq_res;
111 	int		mem_rid;
112 	int		irq_rid;
113 
114 	struct cdev	*sc_cdev;
115 
116 	struct sx 	dev_lock;
117 	struct cv	buf_cv;
118 
119 	void 		*intr_cookie;
120 	int 		intr_type;	/* Current event type */
121 	bool 		interrupts;
122 
123 	uint8_t 	*buf;
124 	size_t		pending_data_length;
125 	lwpid_t		owner_tid;
126 
127 	struct callout 	discard_buffer_callout;
128 #ifdef TPM_HARVEST
129 	struct timeout_task 	harvest_task;
130 #endif
131 
132 	int		(*transmit)(struct tpm_sc *, size_t);
133 };
134 
135 int tpm20_suspend(device_t dev);
136 int tpm20_shutdown(device_t dev);
137 int32_t tpm20_get_timeout(uint32_t command);
138 int tpm20_init(struct tpm_sc *sc);
139 void tpm20_release(struct tpm_sc *sc);
140 
141 /* Small helper routines for io ops */
142 static inline uint8_t
143 RD1(struct tpm_sc *sc, bus_size_t off)
144 {
145 
146 	return (bus_read_1(sc->mem_res, off));
147 }
148 static inline uint32_t
149 RD4(struct tpm_sc *sc, bus_size_t off)
150 {
151 
152 	return (bus_read_4(sc->mem_res, off));
153 }
154 #ifdef __amd64__
155 static inline uint64_t
156 RD8(struct tpm_sc *sc, bus_size_t off)
157 {
158 
159 	return (bus_read_8(sc->mem_res, off));
160 }
161 #endif
162 static inline void
163 WR1(struct tpm_sc *sc, bus_size_t off, uint8_t val)
164 {
165 
166 	bus_write_1(sc->mem_res, off, val);
167 }
168 static inline void
169 WR4(struct tpm_sc *sc, bus_size_t off, uint32_t val)
170 {
171 
172 	bus_write_4(sc->mem_res, off, val);
173 }
174 static inline void
175 AND4(struct tpm_sc *sc, bus_size_t off, uint32_t val)
176 {
177 
178 	WR4(sc, off, RD4(sc, off) & val);
179 }
180 static inline void
181 OR1(struct tpm_sc *sc, bus_size_t off, uint8_t val)
182 {
183 
184 	WR1(sc, off, RD1(sc, off) | val);
185 }
186 static inline void
187 OR4(struct tpm_sc *sc, bus_size_t off, uint32_t val)
188 {
189 
190 	WR4(sc, off, RD4(sc, off) | val);
191 }
192 #endif	/* _TPM20_H_ */
193