xref: /openbmc/qemu/hw/misc/allwinner-sid.c (revision e4ea952f)
16556617cSNiek Linnenbank /*
26556617cSNiek Linnenbank  * Allwinner Security ID emulation
36556617cSNiek Linnenbank  *
46556617cSNiek Linnenbank  * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
56556617cSNiek Linnenbank  *
66556617cSNiek Linnenbank  * This program is free software: you can redistribute it and/or modify
76556617cSNiek Linnenbank  * it under the terms of the GNU General Public License as published by
86556617cSNiek Linnenbank  * the Free Software Foundation, either version 2 of the License, or
96556617cSNiek Linnenbank  * (at your option) any later version.
106556617cSNiek Linnenbank  *
116556617cSNiek Linnenbank  * This program is distributed in the hope that it will be useful,
126556617cSNiek Linnenbank  * but WITHOUT ANY WARRANTY; without even the implied warranty of
136556617cSNiek Linnenbank  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
146556617cSNiek Linnenbank  * GNU General Public License for more details.
156556617cSNiek Linnenbank  *
166556617cSNiek Linnenbank  * You should have received a copy of the GNU General Public License
176556617cSNiek Linnenbank  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
186556617cSNiek Linnenbank  */
196556617cSNiek Linnenbank 
206556617cSNiek Linnenbank #include "qemu/osdep.h"
216556617cSNiek Linnenbank #include "qemu/units.h"
226556617cSNiek Linnenbank #include "hw/sysbus.h"
236556617cSNiek Linnenbank #include "migration/vmstate.h"
246556617cSNiek Linnenbank #include "qemu/log.h"
256556617cSNiek Linnenbank #include "qemu/module.h"
266556617cSNiek Linnenbank #include "qemu/guest-random.h"
276556617cSNiek Linnenbank #include "qapi/error.h"
286556617cSNiek Linnenbank #include "hw/qdev-properties.h"
29ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
306556617cSNiek Linnenbank #include "hw/misc/allwinner-sid.h"
316556617cSNiek Linnenbank #include "trace.h"
326556617cSNiek Linnenbank 
336556617cSNiek Linnenbank /* SID register offsets */
346556617cSNiek Linnenbank enum {
356556617cSNiek Linnenbank     REG_PRCTL = 0x40,   /* Control */
366556617cSNiek Linnenbank     REG_RDKEY = 0x60,   /* Read Key */
376556617cSNiek Linnenbank };
386556617cSNiek Linnenbank 
396556617cSNiek Linnenbank /* SID register flags */
406556617cSNiek Linnenbank enum {
416556617cSNiek Linnenbank     REG_PRCTL_WRITE   = 0x0002, /* Unknown write flag */
426556617cSNiek Linnenbank     REG_PRCTL_OP_LOCK = 0xAC00, /* Lock operation */
436556617cSNiek Linnenbank };
446556617cSNiek Linnenbank 
allwinner_sid_read(void * opaque,hwaddr offset,unsigned size)456556617cSNiek Linnenbank static uint64_t allwinner_sid_read(void *opaque, hwaddr offset,
466556617cSNiek Linnenbank                                    unsigned size)
476556617cSNiek Linnenbank {
486556617cSNiek Linnenbank     const AwSidState *s = AW_SID(opaque);
496556617cSNiek Linnenbank     uint64_t val = 0;
506556617cSNiek Linnenbank 
516556617cSNiek Linnenbank     switch (offset) {
526556617cSNiek Linnenbank     case REG_PRCTL:    /* Control */
536556617cSNiek Linnenbank         val = s->control;
546556617cSNiek Linnenbank         break;
556556617cSNiek Linnenbank     case REG_RDKEY:    /* Read Key */
566556617cSNiek Linnenbank         val = s->rdkey;
576556617cSNiek Linnenbank         break;
586556617cSNiek Linnenbank     default:
596556617cSNiek Linnenbank         qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
606556617cSNiek Linnenbank                       __func__, (uint32_t)offset);
616556617cSNiek Linnenbank         return 0;
626556617cSNiek Linnenbank     }
636556617cSNiek Linnenbank 
646556617cSNiek Linnenbank     trace_allwinner_sid_read(offset, val, size);
656556617cSNiek Linnenbank 
666556617cSNiek Linnenbank     return val;
676556617cSNiek Linnenbank }
686556617cSNiek Linnenbank 
allwinner_sid_write(void * opaque,hwaddr offset,uint64_t val,unsigned size)696556617cSNiek Linnenbank static void allwinner_sid_write(void *opaque, hwaddr offset,
706556617cSNiek Linnenbank                                 uint64_t val, unsigned size)
716556617cSNiek Linnenbank {
726556617cSNiek Linnenbank     AwSidState *s = AW_SID(opaque);
736556617cSNiek Linnenbank 
746556617cSNiek Linnenbank     trace_allwinner_sid_write(offset, val, size);
756556617cSNiek Linnenbank 
766556617cSNiek Linnenbank     switch (offset) {
776556617cSNiek Linnenbank     case REG_PRCTL:    /* Control */
786556617cSNiek Linnenbank         s->control = val;
796556617cSNiek Linnenbank 
806556617cSNiek Linnenbank         if ((s->control & REG_PRCTL_OP_LOCK) &&
816556617cSNiek Linnenbank             (s->control & REG_PRCTL_WRITE)) {
826556617cSNiek Linnenbank             uint32_t id = s->control >> 16;
836556617cSNiek Linnenbank 
846556617cSNiek Linnenbank             if (id <= sizeof(QemuUUID) - sizeof(s->rdkey)) {
856556617cSNiek Linnenbank                 s->rdkey = ldl_be_p(&s->identifier.data[id]);
866556617cSNiek Linnenbank             }
876556617cSNiek Linnenbank         }
886556617cSNiek Linnenbank         s->control &= ~REG_PRCTL_WRITE;
896556617cSNiek Linnenbank         break;
906556617cSNiek Linnenbank     case REG_RDKEY:    /* Read Key */
916556617cSNiek Linnenbank         break;
926556617cSNiek Linnenbank     default:
936556617cSNiek Linnenbank         qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
946556617cSNiek Linnenbank                       __func__, (uint32_t)offset);
956556617cSNiek Linnenbank         break;
966556617cSNiek Linnenbank     }
976556617cSNiek Linnenbank }
986556617cSNiek Linnenbank 
996556617cSNiek Linnenbank static const MemoryRegionOps allwinner_sid_ops = {
1006556617cSNiek Linnenbank     .read = allwinner_sid_read,
1016556617cSNiek Linnenbank     .write = allwinner_sid_write,
1026556617cSNiek Linnenbank     .endianness = DEVICE_NATIVE_ENDIAN,
1036556617cSNiek Linnenbank     .valid = {
1046556617cSNiek Linnenbank         .min_access_size = 4,
1056556617cSNiek Linnenbank         .max_access_size = 4,
1066556617cSNiek Linnenbank     },
1076556617cSNiek Linnenbank     .impl.min_access_size = 4,
1086556617cSNiek Linnenbank };
1096556617cSNiek Linnenbank 
allwinner_sid_reset(DeviceState * dev)1106556617cSNiek Linnenbank static void allwinner_sid_reset(DeviceState *dev)
1116556617cSNiek Linnenbank {
1126556617cSNiek Linnenbank     AwSidState *s = AW_SID(dev);
1136556617cSNiek Linnenbank 
1146556617cSNiek Linnenbank     /* Set default values for registers */
1156556617cSNiek Linnenbank     s->control = 0;
1166556617cSNiek Linnenbank     s->rdkey = 0;
1176556617cSNiek Linnenbank }
1186556617cSNiek Linnenbank 
allwinner_sid_init(Object * obj)1196556617cSNiek Linnenbank static void allwinner_sid_init(Object *obj)
1206556617cSNiek Linnenbank {
1216556617cSNiek Linnenbank     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1226556617cSNiek Linnenbank     AwSidState *s = AW_SID(obj);
1236556617cSNiek Linnenbank 
1246556617cSNiek Linnenbank     /* Memory mapping */
1256556617cSNiek Linnenbank     memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_sid_ops, s,
1266556617cSNiek Linnenbank                            TYPE_AW_SID, 1 * KiB);
1276556617cSNiek Linnenbank     sysbus_init_mmio(sbd, &s->iomem);
1286556617cSNiek Linnenbank }
1296556617cSNiek Linnenbank 
1306556617cSNiek Linnenbank static Property allwinner_sid_properties[] = {
1316556617cSNiek Linnenbank     DEFINE_PROP_UUID_NODEFAULT("identifier", AwSidState, identifier),
1326556617cSNiek Linnenbank     DEFINE_PROP_END_OF_LIST()
1336556617cSNiek Linnenbank };
1346556617cSNiek Linnenbank 
1356556617cSNiek Linnenbank static const VMStateDescription allwinner_sid_vmstate = {
1366556617cSNiek Linnenbank     .name = "allwinner-sid",
1376556617cSNiek Linnenbank     .version_id = 1,
1386556617cSNiek Linnenbank     .minimum_version_id = 1,
139*e4ea952fSRichard Henderson     .fields = (const VMStateField[]) {
1406556617cSNiek Linnenbank         VMSTATE_UINT32(control, AwSidState),
1416556617cSNiek Linnenbank         VMSTATE_UINT32(rdkey, AwSidState),
1426556617cSNiek Linnenbank         VMSTATE_UINT8_ARRAY_V(identifier.data, AwSidState, sizeof(QemuUUID), 1),
1436556617cSNiek Linnenbank         VMSTATE_END_OF_LIST()
1446556617cSNiek Linnenbank     }
1456556617cSNiek Linnenbank };
1466556617cSNiek Linnenbank 
allwinner_sid_class_init(ObjectClass * klass,void * data)1476556617cSNiek Linnenbank static void allwinner_sid_class_init(ObjectClass *klass, void *data)
1486556617cSNiek Linnenbank {
1496556617cSNiek Linnenbank     DeviceClass *dc = DEVICE_CLASS(klass);
1506556617cSNiek Linnenbank 
1516556617cSNiek Linnenbank     dc->reset = allwinner_sid_reset;
1526556617cSNiek Linnenbank     dc->vmsd = &allwinner_sid_vmstate;
1536556617cSNiek Linnenbank     device_class_set_props(dc, allwinner_sid_properties);
1546556617cSNiek Linnenbank }
1556556617cSNiek Linnenbank 
1566556617cSNiek Linnenbank static const TypeInfo allwinner_sid_info = {
1576556617cSNiek Linnenbank     .name          = TYPE_AW_SID,
1586556617cSNiek Linnenbank     .parent        = TYPE_SYS_BUS_DEVICE,
1596556617cSNiek Linnenbank     .instance_init = allwinner_sid_init,
1606556617cSNiek Linnenbank     .instance_size = sizeof(AwSidState),
1616556617cSNiek Linnenbank     .class_init    = allwinner_sid_class_init,
1626556617cSNiek Linnenbank };
1636556617cSNiek Linnenbank 
allwinner_sid_register(void)1646556617cSNiek Linnenbank static void allwinner_sid_register(void)
1656556617cSNiek Linnenbank {
1666556617cSNiek Linnenbank     type_register_static(&allwinner_sid_info);
1676556617cSNiek Linnenbank }
1686556617cSNiek Linnenbank 
1696556617cSNiek Linnenbank type_init(allwinner_sid_register)
170