]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/tpm/tpm_tis.c
MFC r342084,r342251,r342271,r342285: Introduce TPM2.0 driver
[FreeBSD/FreeBSD.git] / sys / dev / tpm / tpm_tis.c
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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "tpm20.h"
32
33 /*
34  * TIS register space as defined in
35  * TCG_PC_Client_Platform_TPM_Profile_PTP_2.0_r1.03_v22
36  */
37 #define TPM_ACCESS                      0x0
38 #define TPM_INT_ENABLE          0x8
39 #define TPM_INT_VECTOR          0xc
40 #define TPM_INT_STS                     0x10
41 #define TPM_INTF_CAPS           0x14
42 #define TPM_STS                         0x18
43 #define TPM_DATA_FIFO           0x24
44 #define TPM_INTF_ID                     0x30
45 #define TPM_XDATA_FIFO          0x80
46 #define TPM_DID_VID                     0xF00
47 #define TPM_RID                         0xF04
48
49 #define TPM_ACCESS_LOC_REQ                      BIT(1)
50 #define TPM_ACCESS_LOC_Seize            BIT(3)
51 #define TPM_ACCESS_LOC_ACTIVE           BIT(5)
52 #define TPM_ACCESS_LOC_RELINQUISH       BIT(5)
53 #define TPM_ACCESS_VALID                        BIT(7)
54
55 #define TPM_INT_ENABLE_GLOBAL_ENABLE    BIT(31)
56 #define TPM_INT_ENABLE_CMD_RDY                  BIT(7)
57 #define TPM_INT_ENABLE_LOC_CHANGE               BIT(2)
58 #define TPM_INT_ENABLE_STS_VALID                BIT(1)
59 #define TPM_INT_ENABLE_DATA_AVAIL               BIT(0)
60
61 #define TPM_INT_STS_CMD_RDY             BIT(7)
62 #define TPM_INT_STS_LOC_CHANGE  BIT(2)
63 #define TPM_INT_STS_VALID               BIT(1)
64 #define TPM_INT_STS_DATA_AVAIL  BIT(0)
65
66 #define TPM_INTF_CAPS_VERSION   0x70000000
67 #define TPM_INTF_CAPS_TPM20             0x30000000
68
69 #define TPM_STS_VALID                   BIT(7)
70 #define TPM_STS_CMD_RDY                 BIT(6)
71 #define TPM_STS_CMD_START               BIT(5)
72 #define TPM_STS_DATA_AVAIL              BIT(4)
73 #define TPM_STS_DATA_EXPECTED   BIT(3)
74 #define TPM_STS_BURST_MASK              0xFFFF00
75 #define TPM_STS_BURST_OFFSET    0x8
76
77 static int tpmtis_transmit(struct tpm_sc *sc, size_t length);
78
79 static int tpmtis_acpi_probe(device_t dev);
80 static int tpmtis_attach(device_t dev);
81 static int tpmtis_detach(device_t dev);
82
83 static void tpmtis_intr_handler(void *arg);
84
85 static ACPI_STATUS tpmtis_get_SIRQ_channel(ACPI_RESOURCE *res, void *arg);
86 static bool tpmtis_setup_intr(struct tpm_sc *sc);
87
88 static bool tpmtis_read_bytes(struct tpm_sc *sc, size_t count, uint8_t *buf);
89 static bool tpmtis_write_bytes(struct tpm_sc *sc, size_t count, uint8_t *buf);
90 static bool tpmtis_request_locality(struct tpm_sc *sc, int locality);
91 static void tpmtis_relinquish_locality(struct tpm_sc *sc);
92 static bool tpmtis_go_ready(struct tpm_sc *sc);
93
94 static bool tpm_wait_for_u32(struct tpm_sc *sc, bus_size_t off,
95     uint32_t mask, uint32_t val, int32_t timeout);
96 static uint16_t tpmtis_wait_for_burst(struct tpm_sc *sc);
97
98 char *tpmtis_ids[] = {"MSFT0101", NULL};
99
100 static int
101 tpmtis_acpi_probe(device_t dev)
102 {
103         struct resource *res;
104         int rid = 0;
105         uint32_t caps;
106
107         if (ACPI_ID_PROBE(device_get_parent(dev), dev, tpmtis_ids) == NULL)
108                 return (ENXIO);
109
110         /* Check if device is in TPM 2.0 TIS mode */
111         res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
112         if (res == NULL)
113                 return (ENXIO);
114
115         caps = bus_read_4(res, TPM_INTF_CAPS);
116         bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
117         if ((caps & TPM_INTF_CAPS_VERSION) != TPM_INTF_CAPS_TPM20)
118                 return (ENXIO);
119
120         device_set_desc(dev, "Trusted Platform Module 2.0, FIFO mode");
121         return (BUS_PROBE_DEFAULT);
122 }
123
124 static int
125 tpmtis_attach(device_t dev)
126 {
127         struct tpm_sc *sc;
128         int result;
129
130         sc = device_get_softc(dev);
131         sc->dev = dev;
132
133         sc->mem_rid = 0;
134         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
135                     RF_ACTIVE);
136         if (sc->mem_res == NULL)
137                 return (ENXIO);
138
139         sc->irq_rid = 0;
140         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
141                     RF_ACTIVE | RF_SHAREABLE);
142         if (sc->irq_res != NULL) {
143                 if (bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
144                     NULL, tpmtis_intr_handler, sc, &sc->intr_cookie))
145                         sc->interrupts = false;
146                 else
147                         sc->interrupts = tpmtis_setup_intr(sc);
148         } else {
149                 sc->interrupts = false;
150         }
151
152         sc->intr_type = -1;
153
154         sc->transmit = tpmtis_transmit;
155
156         result = tpm20_init(sc);
157         if (result != 0)
158                 tpmtis_detach(dev);
159
160         return (result);
161 }
162
163 static int
164 tpmtis_detach(device_t dev)
165 {
166         struct tpm_sc *sc;
167
168         sc = device_get_softc(dev);
169
170         if (sc->intr_cookie != NULL)
171                 bus_teardown_intr(dev, sc->irq_res, sc->intr_cookie);
172
173         if (sc->irq_res != NULL)
174                 bus_release_resource(dev, SYS_RES_IRQ,
175                     sc->irq_rid, sc->irq_res);
176
177         if (sc->mem_res != NULL)
178                 bus_release_resource(dev, SYS_RES_MEMORY,
179                     sc->mem_rid, sc->mem_res);
180
181         tpm20_release(sc);
182         return (0);
183 }
184
185 static ACPI_STATUS
186 tpmtis_get_SIRQ_channel(ACPI_RESOURCE *res, void *arg)
187 {
188         struct tpm_sc *sc;
189         uint8_t channel;
190
191         sc = (struct tpm_sc *)arg;
192
193         switch (res->Type) {
194         case ACPI_RESOURCE_TYPE_IRQ:
195                 channel = res->Data.Irq.Interrupts[0];
196                 break;
197         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
198                 channel = res->Data.ExtendedIrq.Interrupts[0];
199                 break;
200         default:
201                 return (AE_OK);
202         }
203
204         WR1(sc, TPM_INT_VECTOR, channel);
205         return (AE_OK);
206 }
207
208 static bool
209 tpmtis_setup_intr(struct tpm_sc *sc)
210 {
211         ACPI_STATUS status;
212         ACPI_HANDLE handle;
213         uint32_t irq_mask;
214
215         handle = acpi_get_handle(sc->dev);
216
217         if(!tpmtis_request_locality(sc, 0))
218                 return (false);
219
220         irq_mask = RD4(sc, TPM_INT_ENABLE);
221         irq_mask |= TPM_INT_ENABLE_GLOBAL_ENABLE |
222             TPM_INT_ENABLE_DATA_AVAIL |
223             TPM_INT_ENABLE_LOC_CHANGE |
224             TPM_INT_ENABLE_CMD_RDY |
225             TPM_INT_ENABLE_STS_VALID;
226         WR4(sc, TPM_INT_ENABLE, irq_mask);
227
228         status = AcpiWalkResources(handle, "_CRS",
229             tpmtis_get_SIRQ_channel, (void *)sc);
230
231         tpmtis_relinquish_locality(sc);
232
233         return (ACPI_SUCCESS(status));
234 }
235
236 static void
237 tpmtis_intr_handler(void *arg)
238 {
239         struct tpm_sc *sc;
240         uint32_t status;
241
242         sc = (struct tpm_sc *)arg;
243         status = RD4(sc, TPM_INT_STS);
244
245         WR4(sc, TPM_INT_STS, status);
246         if (sc->intr_type != -1 && sc->intr_type & status)
247                 wakeup(sc);
248 }
249
250 static bool
251 tpm_wait_for_u32(struct tpm_sc *sc, bus_size_t off, uint32_t mask, uint32_t val,
252     int32_t timeout)
253 {
254
255         /* Check for condition */
256         if ((RD4(sc, off) & mask) == val)
257                 return (true);
258
259         /* If interrupts are enabled sleep for timeout duration */
260         if(sc->interrupts && sc->intr_type != -1) {
261                 tsleep(sc, PWAIT, "TPM WITH INTERRUPTS", timeout / tick);
262
263                 sc->intr_type = -1;
264                 return ((RD4(sc, off) & mask) == val);
265         }
266
267         /* If we don't have interrupts poll the device every tick */
268         while (timeout > 0) {
269                 if ((RD4(sc, off) & mask) == val)
270                         return (true);
271
272                 pause("TPM POLLING", 1);
273                 timeout -= tick;
274         }
275         return (false);
276 }
277
278 static uint16_t
279 tpmtis_wait_for_burst(struct tpm_sc *sc)
280 {
281         int timeout;
282         uint16_t burst_count;
283
284         timeout = TPM_TIMEOUT_A;
285
286         while (timeout-- > 0) {
287                 burst_count = (RD4(sc, TPM_STS) & TPM_STS_BURST_MASK) >>
288                     TPM_STS_BURST_OFFSET;
289                 if (burst_count > 0)
290                         break;
291
292                 DELAY(1);
293         }
294         return (burst_count);
295 }
296
297 static bool
298 tpmtis_read_bytes(struct tpm_sc *sc, size_t count, uint8_t *buf)
299 {
300         uint16_t burst_count;
301
302         while (count > 0) {
303                 burst_count = tpmtis_wait_for_burst(sc);
304                 if (burst_count == 0)
305                         return (false);
306
307                 burst_count = MIN(burst_count, count);
308                 count -= burst_count;
309
310                 while (burst_count-- > 0)
311                         *buf++ = RD1(sc, TPM_DATA_FIFO);
312         }
313
314         return (true);
315 }
316
317 static bool
318 tpmtis_write_bytes(struct tpm_sc *sc, size_t count, uint8_t *buf)
319 {
320         uint16_t burst_count;
321
322         while (count > 0) {
323                 burst_count = tpmtis_wait_for_burst(sc);
324                 if (burst_count == 0)
325                         return (false);
326
327                 burst_count = MIN(burst_count, count);
328                 count -= burst_count;
329
330                 while (burst_count-- > 0)
331                         WR1(sc, TPM_DATA_FIFO, *buf++);
332         }
333
334         return (true);
335 }
336
337
338 static bool
339 tpmtis_request_locality(struct tpm_sc *sc, int locality)
340 {
341         uint8_t mask;
342         int timeout;
343
344         /* Currently we only support Locality 0 */
345         if (locality != 0)
346                 return (false);
347
348         mask = TPM_ACCESS_LOC_ACTIVE | TPM_ACCESS_VALID;
349         timeout = TPM_TIMEOUT_A;
350         sc->intr_type = TPM_INT_STS_LOC_CHANGE;
351
352         WR1(sc, TPM_ACCESS, TPM_ACCESS_LOC_REQ);
353         bus_barrier(sc->mem_res, TPM_ACCESS, 1, BUS_SPACE_BARRIER_WRITE);
354         if(sc->interrupts) {
355                 tsleep(sc, PWAIT, "TPMLOCREQUEST with INTR", timeout / tick);
356                 return ((RD1(sc, TPM_ACCESS) & mask) == mask);
357         } else  {
358                 while(timeout > 0) {
359                         if ((RD1(sc, TPM_ACCESS) & mask) == mask)
360                                 return (true);
361
362                         pause("TPMLOCREQUEST POLLING", 1);
363                         timeout -= tick;
364                 }
365         }
366
367         return (false);
368 }
369
370 static void
371 tpmtis_relinquish_locality(struct tpm_sc *sc)
372 {
373
374         /*
375          * Interrupts can only be cleared when a locality is active.
376          * Clear them now in case interrupt handler didn't make it in time.
377          */
378         if(sc->interrupts)
379                 AND4(sc, TPM_INT_STS, RD4(sc, TPM_INT_STS));
380
381         OR1(sc, TPM_ACCESS, TPM_ACCESS_LOC_RELINQUISH);
382 }
383
384 static bool
385 tpmtis_go_ready(struct tpm_sc *sc)
386 {
387         uint32_t mask;
388
389         mask = TPM_STS_CMD_RDY;
390         sc->intr_type = TPM_INT_STS_CMD_RDY;
391
392         OR4(sc, TPM_STS, TPM_STS_CMD_RDY);
393         bus_barrier(sc->mem_res, TPM_STS, 4, BUS_SPACE_BARRIER_WRITE);
394         if (!tpm_wait_for_u32(sc, TPM_STS, mask, mask, TPM_TIMEOUT_B))
395                 return (false);
396
397         AND4(sc, TPM_STS, ~TPM_STS_CMD_RDY);
398         return (true);
399 }
400
401 static int
402 tpmtis_transmit(struct tpm_sc *sc, size_t length)
403 {
404         size_t bytes_available;
405         uint32_t mask, curr_cmd;
406         int timeout;
407
408         sx_assert(&sc->dev_lock, SA_XLOCKED);
409
410         if (!tpmtis_request_locality(sc, 0)) {
411                 device_printf(sc->dev,
412                     "Failed to obtain locality\n");
413                 return (EIO);
414         }
415         if (!tpmtis_go_ready(sc)) {
416                 device_printf(sc->dev,
417                     "Failed to switch to ready state\n");
418                 return (EIO);
419         }
420         if (!tpmtis_write_bytes(sc, length, sc->buf)) {
421                 device_printf(sc->dev,
422                     "Failed to write cmd to device\n");
423                 return (EIO);
424         }
425
426         mask = TPM_STS_VALID;
427         sc->intr_type = TPM_INT_STS_VALID;
428         if (!tpm_wait_for_u32(sc, TPM_STS, mask, mask, TPM_TIMEOUT_C)) {
429                 device_printf(sc->dev,
430                     "Timeout while waiting for valid bit\n");
431                 return (EIO);
432         }
433         if (RD4(sc, TPM_STS) & TPM_STS_DATA_EXPECTED) {
434                 device_printf(sc->dev,
435                     "Device expects more data even though we already"
436                     " sent everything we had\n");
437                 return (EIO);
438         }
439
440         /*
441          * Calculate timeout for current command.
442          * Command code is passed in bytes 6-10.
443          */
444         curr_cmd = be32toh(*(uint32_t *) (&sc->buf[6]));
445         timeout = tpm20_get_timeout(curr_cmd);
446
447         WR4(sc, TPM_STS, TPM_STS_CMD_START);
448         bus_barrier(sc->mem_res, TPM_STS, 4, BUS_SPACE_BARRIER_WRITE);
449
450         mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
451         sc->intr_type = TPM_INT_STS_DATA_AVAIL;
452         if (!tpm_wait_for_u32(sc, TPM_STS, mask, mask, timeout)) {
453                 device_printf(sc->dev,
454                     "Timeout while waiting for device to process cmd\n");
455                 /*
456                  * Switching to ready state also cancels processing
457                  * current command
458                  */
459                 if (!tpmtis_go_ready(sc))
460                         return (EIO);
461
462                 /*
463                  * After canceling a command we should get a response,
464                  * check if there is one.
465                  */
466                 sc->intr_type = TPM_INT_STS_DATA_AVAIL;
467                 if (!tpm_wait_for_u32(sc, TPM_STS, mask, mask, TPM_TIMEOUT_C))
468                         return (EIO);
469         }
470         /* Read response header. Length is passed in bytes 2 - 6. */
471         if(!tpmtis_read_bytes(sc, TPM_HEADER_SIZE, sc->buf)) {
472                 device_printf(sc->dev,
473                     "Failed to read response header\n");
474                 return (EIO);
475         }
476         bytes_available = be32toh(*(uint32_t *) (&sc->buf[2]));
477
478         if (bytes_available > TPM_BUFSIZE || bytes_available < TPM_HEADER_SIZE) {
479                 device_printf(sc->dev,
480                     "Incorrect response size: %zu\n",
481                     bytes_available);
482                 return (EIO);
483         }
484         if(!tpmtis_read_bytes(sc, bytes_available - TPM_HEADER_SIZE,
485             &sc->buf[TPM_HEADER_SIZE])) {
486                 device_printf(sc->dev,
487                     "Failed to read response\n");
488                 return (EIO);
489         }
490         tpmtis_relinquish_locality(sc);
491         sc->pending_data_length = bytes_available;
492
493         return (0);
494 }
495
496 /* ACPI Driver */
497 static device_method_t tpmtis_methods[] = {
498         DEVMETHOD(device_probe,         tpmtis_acpi_probe),
499         DEVMETHOD(device_attach,        tpmtis_attach),
500         DEVMETHOD(device_detach,        tpmtis_detach),
501         DEVMETHOD(device_shutdown,      tpm20_shutdown),
502         DEVMETHOD(device_suspend,       tpm20_suspend),
503         {0, 0}
504 };
505 static driver_t tpmtis_driver = {
506         "tpmtis", tpmtis_methods, sizeof(struct tpm_sc),
507 };
508
509 devclass_t tpmtis_devclass;
510 DRIVER_MODULE(tpmtis, acpi, tpmtis_driver, tpmtis_devclass, 0, 0);