2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
5 * This file models the system mailboxes, which are used for
6 * communication with low-bandwidth GPU peripherals. Refs:
7 * https://github.com/raspberrypi/firmware/wiki/Mailboxes
8 * https://github.com/raspberrypi/firmware/wiki/Accessing-mailboxes
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "hw/misc/bcm2835_mbox.h"
15 #define MAIL0_PEEK 0x90
16 #define MAIL0_SENDER 0x94
17 #define MAIL1_STATUS 0xb8
19 /* Mailbox status register */
20 #define MAIL0_STATUS 0x98
21 #define ARM_MS_FULL 0x80000000
22 #define ARM_MS_EMPTY 0x40000000
23 #define ARM_MS_LEVEL 0x400000FF /* Max. value depends on mailbox depth */
25 /* MAILBOX config/status register */
26 #define MAIL0_CONFIG 0x9c
27 /* ANY write to this register clears the error bits! */
28 #define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mbox irq enable: has data */
29 #define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mbox irq enable: has space */
30 #define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mbox irq enable: Opp is empty */
31 #define ARM_MC_MAIL_CLEAR 0x00000008 /* mbox clear write 1, then 0 */
32 #define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mbox irq pending: has space */
33 #define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mbox irq pending: Opp is empty */
34 #define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mbox irq pending */
36 #define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
37 #define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
38 #define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
40 static void mbox_update_status(BCM2835Mbox
*mb
)
42 mb
->status
&= ~(ARM_MS_EMPTY
| ARM_MS_FULL
);
44 mb
->status
|= ARM_MS_EMPTY
;
45 } else if (mb
->count
== MBOX_SIZE
) {
46 mb
->status
|= ARM_MS_FULL
;
50 static void mbox_reset(BCM2835Mbox
*mb
)
56 for (n
= 0; n
< MBOX_SIZE
; n
++) {
57 mb
->reg
[n
] = MBOX_INVALID_DATA
;
59 mbox_update_status(mb
);
62 static uint32_t mbox_pull(BCM2835Mbox
*mb
, int index
)
67 assert(mb
->count
> 0);
68 assert(index
< mb
->count
);
71 for (n
= index
+ 1; n
< mb
->count
; n
++) {
72 mb
->reg
[n
- 1] = mb
->reg
[n
];
75 mb
->reg
[mb
->count
] = MBOX_INVALID_DATA
;
77 mbox_update_status(mb
);
82 static void mbox_push(BCM2835Mbox
*mb
, uint32_t val
)
84 assert(mb
->count
< MBOX_SIZE
);
85 mb
->reg
[mb
->count
++] = val
;
86 mbox_update_status(mb
);
89 static void bcm2835_mbox_update(BCM2835MboxState
*s
)
95 s
->mbox_irq_disabled
= true;
97 /* Get pending responses and put them in the vc->arm mbox,
98 * as long as it's not full
100 for (n
= 0; n
< MBOX_CHAN_COUNT
; n
++) {
101 while (s
->available
[n
] && !(s
->mbox
[0].status
& ARM_MS_FULL
)) {
102 value
= ldl_le_phys(&s
->mbox_as
, n
<< MBOX_AS_CHAN_SHIFT
);
103 assert(value
!= MBOX_INVALID_DATA
); /* Pending interrupt but no data */
104 mbox_push(&s
->mbox
[0], value
);
108 /* TODO (?): Try to push pending requests from the arm->vc mbox */
110 /* Re-enable calls from the IRQ routine */
111 s
->mbox_irq_disabled
= false;
113 /* Update ARM IRQ status */
115 s
->mbox
[0].config
&= ~ARM_MC_IHAVEDATAIRQPEND
;
116 if (!(s
->mbox
[0].status
& ARM_MS_EMPTY
)) {
117 s
->mbox
[0].config
|= ARM_MC_IHAVEDATAIRQPEND
;
118 if (s
->mbox
[0].config
& ARM_MC_IHAVEDATAIRQEN
) {
122 qemu_set_irq(s
->arm_irq
, set
);
125 static void bcm2835_mbox_set_irq(void *opaque
, int irq
, int level
)
127 BCM2835MboxState
*s
= opaque
;
129 s
->available
[irq
] = level
;
131 /* avoid recursively calling bcm2835_mbox_update when the interrupt
132 * status changes due to the ldl_phys call within that function
134 if (!s
->mbox_irq_disabled
) {
135 bcm2835_mbox_update(s
);
139 static uint64_t bcm2835_mbox_read(void *opaque
, hwaddr offset
, unsigned size
)
141 BCM2835MboxState
*s
= opaque
;
147 case 0x80 ... 0x8c: /* MAIL0_READ */
148 if (s
->mbox
[0].status
& ARM_MS_EMPTY
) {
149 res
= MBOX_INVALID_DATA
;
151 res
= mbox_pull(&s
->mbox
[0], 0);
156 res
= s
->mbox
[0].reg
[0];
163 res
= s
->mbox
[0].status
;
167 res
= s
->mbox
[0].config
;
171 res
= s
->mbox
[1].status
;
175 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
180 bcm2835_mbox_update(s
);
185 static void bcm2835_mbox_write(void *opaque
, hwaddr offset
,
186 uint64_t value
, unsigned size
)
188 BCM2835MboxState
*s
= opaque
;
199 s
->mbox
[0].config
&= ~ARM_MC_IHAVEDATAIRQEN
;
200 s
->mbox
[0].config
|= value
& ARM_MC_IHAVEDATAIRQEN
;
203 case 0xa0 ... 0xac: /* MAIL1_WRITE */
204 if (s
->mbox
[1].status
& ARM_MS_FULL
) {
206 qemu_log_mask(LOG_GUEST_ERROR
, "%s: mailbox full\n", __func__
);
209 if (ch
< MBOX_CHAN_COUNT
) {
210 childaddr
= ch
<< MBOX_AS_CHAN_SHIFT
;
211 if (ldl_le_phys(&s
->mbox_as
, childaddr
+ MBOX_AS_PENDING
)) {
212 /* Child busy, push delayed. Push it in the arm->vc mbox */
213 mbox_push(&s
->mbox
[1], value
);
215 /* Push it directly to the child device */
216 stl_le_phys(&s
->mbox_as
, childaddr
, value
);
219 /* Invalid channel number */
220 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid channel %u\n",
227 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
232 bcm2835_mbox_update(s
);
235 static const MemoryRegionOps bcm2835_mbox_ops
= {
236 .read
= bcm2835_mbox_read
,
237 .write
= bcm2835_mbox_write
,
238 .endianness
= DEVICE_NATIVE_ENDIAN
,
239 .valid
.min_access_size
= 4,
240 .valid
.max_access_size
= 4,
243 /* vmstate of a single mailbox */
244 static const VMStateDescription vmstate_bcm2835_mbox_box
= {
245 .name
= TYPE_BCM2835_MBOX
"_box",
247 .minimum_version_id
= 1,
248 .fields
= (VMStateField
[]) {
249 VMSTATE_UINT32_ARRAY(reg
, BCM2835Mbox
, MBOX_SIZE
),
250 VMSTATE_UINT32(count
, BCM2835Mbox
),
251 VMSTATE_UINT32(status
, BCM2835Mbox
),
252 VMSTATE_UINT32(config
, BCM2835Mbox
),
253 VMSTATE_END_OF_LIST()
257 /* vmstate of the entire device */
258 static const VMStateDescription vmstate_bcm2835_mbox
= {
259 .name
= TYPE_BCM2835_MBOX
,
261 .minimum_version_id
= 1,
262 .minimum_version_id_old
= 1,
263 .fields
= (VMStateField
[]) {
264 VMSTATE_BOOL_ARRAY(available
, BCM2835MboxState
, MBOX_CHAN_COUNT
),
265 VMSTATE_STRUCT_ARRAY(mbox
, BCM2835MboxState
, 2, 1,
266 vmstate_bcm2835_mbox_box
, BCM2835Mbox
),
267 VMSTATE_END_OF_LIST()
271 static void bcm2835_mbox_init(Object
*obj
)
273 BCM2835MboxState
*s
= BCM2835_MBOX(obj
);
275 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_mbox_ops
, s
,
276 TYPE_BCM2835_MBOX
, 0x400);
277 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
278 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->arm_irq
);
279 qdev_init_gpio_in(DEVICE(s
), bcm2835_mbox_set_irq
, MBOX_CHAN_COUNT
);
282 static void bcm2835_mbox_reset(DeviceState
*dev
)
284 BCM2835MboxState
*s
= BCM2835_MBOX(dev
);
287 mbox_reset(&s
->mbox
[0]);
288 mbox_reset(&s
->mbox
[1]);
289 s
->mbox_irq_disabled
= false;
290 for (n
= 0; n
< MBOX_CHAN_COUNT
; n
++) {
291 s
->available
[n
] = false;
295 static void bcm2835_mbox_realize(DeviceState
*dev
, Error
**errp
)
297 BCM2835MboxState
*s
= BCM2835_MBOX(dev
);
301 obj
= object_property_get_link(OBJECT(dev
), "mbox-mr", &err
);
303 error_setg(errp
, "%s: required mbox-mr link not found: %s",
304 __func__
, error_get_pretty(err
));
308 s
->mbox_mr
= MEMORY_REGION(obj
);
309 address_space_init(&s
->mbox_as
, s
->mbox_mr
, NULL
);
310 bcm2835_mbox_reset(dev
);
313 static void bcm2835_mbox_class_init(ObjectClass
*klass
, void *data
)
315 DeviceClass
*dc
= DEVICE_CLASS(klass
);
317 dc
->realize
= bcm2835_mbox_realize
;
318 dc
->reset
= bcm2835_mbox_reset
;
319 dc
->vmsd
= &vmstate_bcm2835_mbox
;
322 static TypeInfo bcm2835_mbox_info
= {
323 .name
= TYPE_BCM2835_MBOX
,
324 .parent
= TYPE_SYS_BUS_DEVICE
,
325 .instance_size
= sizeof(BCM2835MboxState
),
326 .class_init
= bcm2835_mbox_class_init
,
327 .instance_init
= bcm2835_mbox_init
,
330 static void bcm2835_mbox_register_types(void)
332 type_register_static(&bcm2835_mbox_info
);
335 type_init(bcm2835_mbox_register_types
)