qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

tpm: Use TPMState as a common struct

As we plan to introduce a SysBus TPM TIS device, let's
make the TPMState a common struct usable by both the
ISADevice and the SysBusDevice. TPMStateISA embeds the
struct and inherits from the ISADevice.

The prototype of functions bound to be used by both
the ISA and SysBus devices is changed to take TPMState
handle.

A bunch of structs also are renamed to be specialized
for the ISA device. Besides those transformations, no
functional change is expected.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Message-id: 20200305165149.618-3-eric.auger@redhat.com
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>

authored by

Eric Auger and committed by
Stefan Berger
ca75c421 43bc7f84

+91 -55
+91 -55
hw/tpm/tpm_tis.c
··· 65 65 } TPMLocality; 66 66 67 67 typedef struct TPMState { 68 - ISADevice busdev; 69 68 MemoryRegion mmio; 70 69 71 70 unsigned char buffer[TPM_TIS_BUFFER_MAX]; ··· 91 90 TPMPPI ppi; 92 91 } TPMState; 93 92 94 - #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS_ISA) 93 + typedef struct TPMStateISA { 94 + /*< private >*/ 95 + ISADevice parent_obj; 96 + 97 + /*< public >*/ 98 + TPMState state; /* not a QOM object */ 99 + } TPMStateISA; 100 + 101 + #define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA) 95 102 96 103 #define DEBUG_TIS 0 97 104 ··· 281 288 /* 282 289 * Callback from the TPM to indicate that the response was received. 283 290 */ 284 - static void tpm_tis_request_completed(TPMIf *ti, int ret) 291 + static void tpm_tis_request_completed(TPMState *s, int ret) 285 292 { 286 - TPMState *s = TPM(ti); 287 293 uint8_t locty = s->cmd.locty; 288 294 uint8_t l; 289 295 ··· 338 344 } 339 345 340 346 #ifdef DEBUG_TIS 341 - static void tpm_tis_dump_state(void *opaque, hwaddr addr) 347 + static void tpm_tis_dump_state(TPMState *s, hwaddr addr) 342 348 { 343 349 static const unsigned regs[] = { 344 350 TPM_TIS_REG_ACCESS, ··· 353 359 int idx; 354 360 uint8_t locty = tpm_tis_locality_from_addr(addr); 355 361 hwaddr base = addr & ~0xfff; 356 - TPMState *s = opaque; 357 362 358 363 printf("tpm_tis: active locality : %d\n" 359 364 "tpm_tis: state of locality %d : %d\n" ··· 363 368 364 369 for (idx = 0; regs[idx] != 0xfff; idx++) { 365 370 printf("tpm_tis: 0x%04x : 0x%08x\n", regs[idx], 366 - (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4)); 371 + (int)tpm_tis_mmio_read(s, base + regs[idx], 4)); 367 372 } 368 373 369 374 printf("tpm_tis: r/w offset : %d\n" ··· 488 493 break; 489 494 #ifdef DEBUG_TIS 490 495 case TPM_TIS_REG_DEBUG: 491 - tpm_tis_dump_state(opaque, addr); 496 + tpm_tis_dump_state(s, addr); 492 497 break; 493 498 #endif 494 499 } ··· 835 840 /* 836 841 * Get the TPMVersion of the backend device being used 837 842 */ 838 - static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti) 843 + static enum TPMVersion tpm_tis_get_tpm_version(TPMState *s) 839 844 { 840 - TPMState *s = TPM(ti); 841 - 842 845 if (tpm_backend_had_startup_error(s->be_driver)) { 843 846 return TPM_VERSION_UNSPEC; 844 847 } ··· 850 853 * This function is called when the machine starts, resets or due to 851 854 * S3 resume. 852 855 */ 853 - static void tpm_tis_reset(DeviceState *dev) 856 + static void tpm_tis_reset(TPMState *s) 854 857 { 855 - TPMState *s = TPM(dev); 856 858 int c; 857 859 858 860 s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver); ··· 896 898 897 899 /* persistent state handling */ 898 900 899 - static int tpm_tis_pre_save(void *opaque) 901 + static int tpm_tis_pre_save(TPMState *s) 900 902 { 901 - TPMState *s = opaque; 902 903 uint8_t locty = s->active_locty; 903 904 904 905 trace_tpm_tis_pre_save(locty, s->rw_offset); 905 906 906 907 if (DEBUG_TIS) { 907 - tpm_tis_dump_state(opaque, 0); 908 + tpm_tis_dump_state(s, 0); 908 909 } 909 910 910 911 /* ··· 929 930 } 930 931 }; 931 932 932 - static const VMStateDescription vmstate_tpm_tis = { 933 + /* ISA */ 934 + 935 + static int tpm_tis_pre_save_isa(void *opaque) 936 + { 937 + TPMStateISA *isadev = opaque; 938 + 939 + return tpm_tis_pre_save(&isadev->state); 940 + } 941 + 942 + static const VMStateDescription vmstate_tpm_tis_isa = { 933 943 .name = "tpm-tis", 934 944 .version_id = 0, 935 - .pre_save = tpm_tis_pre_save, 945 + .pre_save = tpm_tis_pre_save_isa, 936 946 .fields = (VMStateField[]) { 937 - VMSTATE_BUFFER(buffer, TPMState), 938 - VMSTATE_UINT16(rw_offset, TPMState), 939 - VMSTATE_UINT8(active_locty, TPMState), 940 - VMSTATE_UINT8(aborting_locty, TPMState), 941 - VMSTATE_UINT8(next_locty, TPMState), 947 + VMSTATE_BUFFER(state.buffer, TPMStateISA), 948 + VMSTATE_UINT16(state.rw_offset, TPMStateISA), 949 + VMSTATE_UINT8(state.active_locty, TPMStateISA), 950 + VMSTATE_UINT8(state.aborting_locty, TPMStateISA), 951 + VMSTATE_UINT8(state.next_locty, TPMStateISA), 942 952 943 - VMSTATE_STRUCT_ARRAY(loc, TPMState, TPM_TIS_NUM_LOCALITIES, 0, 953 + VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0, 944 954 vmstate_locty, TPMLocality), 945 955 946 956 VMSTATE_END_OF_LIST() 947 957 } 948 958 }; 949 959 950 - static Property tpm_tis_properties[] = { 951 - DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ), 952 - DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver), 953 - DEFINE_PROP_BOOL("ppi", TPMState, ppi_enabled, true), 960 + static void tpm_tis_isa_request_completed(TPMIf *ti, int ret) 961 + { 962 + TPMStateISA *isadev = TPM_TIS_ISA(ti); 963 + TPMState *s = &isadev->state; 964 + 965 + tpm_tis_request_completed(s, ret); 966 + } 967 + 968 + static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti) 969 + { 970 + TPMStateISA *isadev = TPM_TIS_ISA(ti); 971 + TPMState *s = &isadev->state; 972 + 973 + return tpm_tis_get_tpm_version(s); 974 + } 975 + 976 + static void tpm_tis_isa_reset(DeviceState *dev) 977 + { 978 + TPMStateISA *isadev = TPM_TIS_ISA(dev); 979 + TPMState *s = &isadev->state; 980 + 981 + return tpm_tis_reset(s); 982 + } 983 + 984 + static Property tpm_tis_isa_properties[] = { 985 + DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ), 986 + DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver), 987 + DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true), 954 988 DEFINE_PROP_END_OF_LIST(), 955 989 }; 956 990 957 - static void tpm_tis_realizefn(DeviceState *dev, Error **errp) 991 + static void tpm_tis_isa_initfn(Object *obj) 992 + { 993 + TPMStateISA *isadev = TPM_TIS_ISA(obj); 994 + TPMState *s = &isadev->state; 995 + 996 + memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops, 997 + s, "tpm-tis-mmio", 998 + TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT); 999 + } 1000 + 1001 + static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp) 958 1002 { 959 - TPMState *s = TPM(dev); 1003 + TPMStateISA *isadev = TPM_TIS_ISA(dev); 1004 + TPMState *s = &isadev->state; 960 1005 961 1006 if (!tpm_find()) { 962 1007 error_setg(errp, "at most one TPM device is permitted"); ··· 973 1018 return; 974 1019 } 975 1020 976 - isa_init_irq(&s->busdev, &s->irq, s->irq_num); 1021 + isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num); 977 1022 978 1023 memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)), 979 1024 TPM_TIS_ADDR_BASE, &s->mmio); 980 1025 981 1026 if (s->ppi_enabled) { 982 1027 tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)), 983 - TPM_PPI_ADDR_BASE, OBJECT(s)); 1028 + TPM_PPI_ADDR_BASE, OBJECT(dev)); 984 1029 } 985 1030 } 986 1031 987 - static void tpm_tis_initfn(Object *obj) 988 - { 989 - TPMState *s = TPM(obj); 990 - 991 - memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops, 992 - s, "tpm-tis-mmio", 993 - TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT); 994 - } 995 - 996 - static void tpm_tis_class_init(ObjectClass *klass, void *data) 1032 + static void tpm_tis_isa_class_init(ObjectClass *klass, void *data) 997 1033 { 998 1034 DeviceClass *dc = DEVICE_CLASS(klass); 999 1035 TPMIfClass *tc = TPM_IF_CLASS(klass); 1000 1036 1001 - dc->realize = tpm_tis_realizefn; 1002 - device_class_set_props(dc, tpm_tis_properties); 1003 - dc->reset = tpm_tis_reset; 1004 - dc->vmsd = &vmstate_tpm_tis; 1037 + device_class_set_props(dc, tpm_tis_isa_properties); 1038 + dc->vmsd = &vmstate_tpm_tis_isa; 1005 1039 tc->model = TPM_MODEL_TPM_TIS; 1006 - tc->get_version = tpm_tis_get_tpm_version; 1007 - tc->request_completed = tpm_tis_request_completed; 1040 + dc->realize = tpm_tis_isa_realizefn; 1041 + dc->reset = tpm_tis_isa_reset; 1042 + tc->request_completed = tpm_tis_isa_request_completed; 1043 + tc->get_version = tpm_tis_isa_get_tpm_version; 1008 1044 } 1009 1045 1010 - static const TypeInfo tpm_tis_info = { 1046 + static const TypeInfo tpm_tis_isa_info = { 1011 1047 .name = TYPE_TPM_TIS_ISA, 1012 1048 .parent = TYPE_ISA_DEVICE, 1013 - .instance_size = sizeof(TPMState), 1014 - .instance_init = tpm_tis_initfn, 1015 - .class_init = tpm_tis_class_init, 1049 + .instance_size = sizeof(TPMStateISA), 1050 + .instance_init = tpm_tis_isa_initfn, 1051 + .class_init = tpm_tis_isa_class_init, 1016 1052 .interfaces = (InterfaceInfo[]) { 1017 1053 { TYPE_TPM_IF }, 1018 1054 { } 1019 1055 } 1020 1056 }; 1021 1057 1022 - static void tpm_tis_register(void) 1058 + static void tpm_tis_isa_register(void) 1023 1059 { 1024 - type_register_static(&tpm_tis_info); 1060 + type_register_static(&tpm_tis_isa_info); 1025 1061 } 1026 1062 1027 - type_init(tpm_tis_register) 1063 + type_init(tpm_tis_isa_register)