2 * Allwinner Security ID emulation
4 * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
22 #include "hw/sysbus.h"
23 #include "migration/vmstate.h"
25 #include "qemu/module.h"
26 #include "qemu/guest-random.h"
27 #include "qapi/error.h"
28 #include "hw/qdev-properties.h"
29 #include "hw/misc/allwinner-sid.h"
32 /* SID register offsets */
34 REG_PRCTL
= 0x40, /* Control */
35 REG_RDKEY
= 0x60, /* Read Key */
38 /* SID register flags */
40 REG_PRCTL_WRITE
= 0x0002, /* Unknown write flag */
41 REG_PRCTL_OP_LOCK
= 0xAC00, /* Lock operation */
44 static uint64_t allwinner_sid_read(void *opaque
, hwaddr offset
,
47 const AwSidState
*s
= AW_SID(opaque
);
51 case REG_PRCTL
: /* Control */
54 case REG_RDKEY
: /* Read Key */
58 qemu_log_mask(LOG_GUEST_ERROR
, "%s: out-of-bounds offset 0x%04x\n",
59 __func__
, (uint32_t)offset
);
63 trace_allwinner_sid_read(offset
, val
, size
);
68 static void allwinner_sid_write(void *opaque
, hwaddr offset
,
69 uint64_t val
, unsigned size
)
71 AwSidState
*s
= AW_SID(opaque
);
73 trace_allwinner_sid_write(offset
, val
, size
);
76 case REG_PRCTL
: /* Control */
79 if ((s
->control
& REG_PRCTL_OP_LOCK
) &&
80 (s
->control
& REG_PRCTL_WRITE
)) {
81 uint32_t id
= s
->control
>> 16;
83 if (id
<= sizeof(QemuUUID
) - sizeof(s
->rdkey
)) {
84 s
->rdkey
= ldl_be_p(&s
->identifier
.data
[id
]);
87 s
->control
&= ~REG_PRCTL_WRITE
;
89 case REG_RDKEY
: /* Read Key */
92 qemu_log_mask(LOG_GUEST_ERROR
, "%s: out-of-bounds offset 0x%04x\n",
93 __func__
, (uint32_t)offset
);
98 static const MemoryRegionOps allwinner_sid_ops
= {
99 .read
= allwinner_sid_read
,
100 .write
= allwinner_sid_write
,
101 .endianness
= DEVICE_NATIVE_ENDIAN
,
103 .min_access_size
= 4,
104 .max_access_size
= 4,
106 .impl
.min_access_size
= 4,
109 static void allwinner_sid_reset(DeviceState
*dev
)
111 AwSidState
*s
= AW_SID(dev
);
113 /* Set default values for registers */
118 static void allwinner_sid_init(Object
*obj
)
120 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
121 AwSidState
*s
= AW_SID(obj
);
124 memory_region_init_io(&s
->iomem
, OBJECT(s
), &allwinner_sid_ops
, s
,
125 TYPE_AW_SID
, 1 * KiB
);
126 sysbus_init_mmio(sbd
, &s
->iomem
);
129 static Property allwinner_sid_properties
[] = {
130 DEFINE_PROP_UUID_NODEFAULT("identifier", AwSidState
, identifier
),
131 DEFINE_PROP_END_OF_LIST()
134 static const VMStateDescription allwinner_sid_vmstate
= {
135 .name
= "allwinner-sid",
137 .minimum_version_id
= 1,
138 .fields
= (VMStateField
[]) {
139 VMSTATE_UINT32(control
, AwSidState
),
140 VMSTATE_UINT32(rdkey
, AwSidState
),
141 VMSTATE_UINT8_ARRAY_V(identifier
.data
, AwSidState
, sizeof(QemuUUID
), 1),
142 VMSTATE_END_OF_LIST()
146 static void allwinner_sid_class_init(ObjectClass
*klass
, void *data
)
148 DeviceClass
*dc
= DEVICE_CLASS(klass
);
150 dc
->reset
= allwinner_sid_reset
;
151 dc
->vmsd
= &allwinner_sid_vmstate
;
152 device_class_set_props(dc
, allwinner_sid_properties
);
155 static const TypeInfo allwinner_sid_info
= {
157 .parent
= TYPE_SYS_BUS_DEVICE
,
158 .instance_init
= allwinner_sid_init
,
159 .instance_size
= sizeof(AwSidState
),
160 .class_init
= allwinner_sid_class_init
,
163 static void allwinner_sid_register(void)
165 type_register_static(&allwinner_sid_info
);
168 type_init(allwinner_sid_register
)