xref: /openbmc/qemu/hw/fsi/fsi.c (revision 4921d0a7)
1 /*
2  * SPDX-License-Identifier: GPL-2.0-or-later
3  * Copyright (C) 2024 IBM Corp.
4  *
5  * IBM Flexible Service Interface
6  */
7 #include "qemu/osdep.h"
8 #include "qapi/error.h"
9 #include "qemu/log.h"
10 #include "trace.h"
11 
12 #include "hw/fsi/fsi.h"
13 
14 #define TO_REG(x)                               ((x) >> 2)
15 
16 static const TypeInfo fsi_bus_info = {
17     .name = TYPE_FSI_BUS,
18     .parent = TYPE_BUS,
19     .instance_size = sizeof(FSIBus),
20 };
21 
22 static uint64_t fsi_slave_read(void *opaque, hwaddr addr, unsigned size)
23 {
24     FSISlaveState *s = FSI_SLAVE(opaque);
25     int reg = TO_REG(addr);
26 
27     trace_fsi_slave_read(addr, size);
28 
29     if (reg >= FSI_SLAVE_CONTROL_NR_REGS) {
30         qemu_log_mask(LOG_GUEST_ERROR,
31                       "%s: Out of bounds read: 0x%"HWADDR_PRIx" for %u\n",
32                       __func__, addr, size);
33         return 0;
34     }
35 
36     return s->regs[reg];
37 }
38 
39 static void fsi_slave_write(void *opaque, hwaddr addr, uint64_t data,
40                                  unsigned size)
41 {
42     FSISlaveState *s = FSI_SLAVE(opaque);
43     int reg = TO_REG(addr);
44 
45     trace_fsi_slave_write(addr, size, data);
46 
47     if (reg >= FSI_SLAVE_CONTROL_NR_REGS) {
48         qemu_log_mask(LOG_GUEST_ERROR,
49                       "%s: Out of bounds write: 0x%"HWADDR_PRIx" for %u\n",
50                       __func__, addr, size);
51         return;
52     }
53 
54     s->regs[reg] = data;
55 }
56 
57 static const struct MemoryRegionOps fsi_slave_ops = {
58     .read = fsi_slave_read,
59     .write = fsi_slave_write,
60     .endianness = DEVICE_BIG_ENDIAN,
61 };
62 
63 static void fsi_slave_reset(DeviceState *dev)
64 {
65     FSISlaveState *s = FSI_SLAVE(dev);
66 
67     /* Initialize registers */
68     memset(s->regs, 0, sizeof(s->regs));
69 }
70 
71 static void fsi_slave_init(Object *o)
72 {
73     FSISlaveState *s = FSI_SLAVE(o);
74 
75     memory_region_init_io(&s->iomem, OBJECT(s), &fsi_slave_ops,
76                           s, TYPE_FSI_SLAVE, 0x400);
77 }
78 
79 static void fsi_slave_class_init(ObjectClass *klass, void *data)
80 {
81     DeviceClass *dc = DEVICE_CLASS(klass);
82 
83     dc->bus_type = TYPE_FSI_BUS;
84     dc->desc = "FSI Slave";
85     dc->reset = fsi_slave_reset;
86 }
87 
88 static const TypeInfo fsi_slave_info = {
89     .name = TYPE_FSI_SLAVE,
90     .parent = TYPE_DEVICE,
91     .instance_init = fsi_slave_init,
92     .instance_size = sizeof(FSISlaveState),
93     .class_init = fsi_slave_class_init,
94 };
95 
96 static void fsi_register_types(void)
97 {
98     type_register_static(&fsi_bus_info);
99     type_register_static(&fsi_slave_info);
100 }
101 
102 type_init(fsi_register_types);
103