xref: /openbmc/qemu/hw/tpm/tpm_ppi.c (revision 7d87775f)
1 /*
2  * tpm_ppi.c - TPM Physical Presence Interface
3  *
4  * Copyright (C) 2018 IBM Corporation
5  *
6  * Authors:
7  *  Stefan Berger <stefanb@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/memalign.h"
16 #include "qapi/error.h"
17 #include "sysemu/memory_mapping.h"
18 #include "migration/vmstate.h"
19 #include "hw/qdev-core.h"
20 #include "hw/acpi/tpm.h"
21 #include "tpm_ppi.h"
22 #include "trace.h"
23 
24 void tpm_ppi_reset(TPMPPI *tpmppi)
25 {
26     if (tpmppi->buf[0x15a /* movv, docs/specs/tpm.rst */] & 0x1) {
27         GuestPhysBlockList guest_phys_blocks;
28         GuestPhysBlock *block;
29 
30         guest_phys_blocks_init(&guest_phys_blocks);
31         guest_phys_blocks_append(&guest_phys_blocks);
32         QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
33             hwaddr mr_offs = block->host_addr -
34                              (uint8_t *)memory_region_get_ram_ptr(block->mr);
35 
36             trace_tpm_ppi_memset(block->host_addr,
37                                  block->target_end - block->target_start);
38             memset(block->host_addr, 0,
39                    block->target_end - block->target_start);
40             memory_region_set_dirty(block->mr, mr_offs,
41                                     block->target_end - block->target_start);
42         }
43         guest_phys_blocks_free(&guest_phys_blocks);
44     }
45 }
46 
47 void tpm_ppi_init(TPMPPI *tpmppi, MemoryRegion *m,
48                   hwaddr addr, Object *obj)
49 {
50     size_t host_page_size = qemu_real_host_page_size();
51 
52     tpmppi->buf = qemu_memalign(host_page_size,
53                                 ROUND_UP(TPM_PPI_ADDR_SIZE, host_page_size));
54     memory_region_init_ram_device_ptr(&tpmppi->ram, obj, "tpm-ppi",
55                                       TPM_PPI_ADDR_SIZE, tpmppi->buf);
56     vmstate_register_ram(&tpmppi->ram, DEVICE(obj));
57 
58     memory_region_add_subregion(m, addr, &tpmppi->ram);
59 }
60