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