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 "hw/misc/bcm2835_mbox.h"
14 /* Mailbox status register (...0x98) */
16 #define MAIL0_PEEK 0x90
17 #define MAIL0_SENDER 0x94
18 #define MAIL1_STATUS 0xb8
20 /* Mailbox status register */
21 #define MAIL0_STATUS 0x98
22 >>>>>>> upstreaming-raspi
23 #define ARM_MS_FULL 0x80000000
24 #define ARM_MS_EMPTY 0x40000000
25 #define ARM_MS_LEVEL 0x400000FF /* Max. value depends on mailbox depth */
28 /* MAILBOX config/status register (...0x9C) */
30 /* MAILBOX config/status register */
31 #define MAIL0_CONFIG 0x9c
32 >>>>>>> upstreaming-raspi
33 /* ANY write to this register clears the error bits! */
34 #define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mbox irq enable: has data */
35 #define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mbox irq enable: has space */
36 #define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mbox irq enable: Opp is empty */
37 #define ARM_MC_MAIL_CLEAR 0x00000008 /* mbox clear write 1, then 0 */
38 #define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mbox irq pending: has space */
39 #define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mbox irq pending: Opp is empty */
40 #define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mbox irq pending */
42 #define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
43 #define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
44 #define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
46 static void mbox_update_status(BCM2835Mbox *mb)
48 mb->status &= ~(ARM_MS_EMPTY | ARM_MS_FULL);
50 mb->status |= ARM_MS_EMPTY;
51 } else if (mb->count == MBOX_SIZE) {
52 mb->status |= ARM_MS_FULL;
57 static void mbox_init(BCM2835Mbox *mb)
59 static void mbox_reset(BCM2835Mbox *mb)
60 >>>>>>> upstreaming-raspi
66 for (n = 0; n < MBOX_SIZE; n++) {
67 mb->reg[n] = MBOX_INVALID_DATA;
69 mbox_update_status(mb);
72 static uint32_t mbox_pull(BCM2835Mbox *mb, int index)
77 assert(mb->count > 0);
78 assert(index < mb->count);
81 for (n = index + 1; n < mb->count; n++) {
82 mb->reg[n - 1] = mb->reg[n];
85 mb->reg[mb->count] = MBOX_INVALID_DATA;
87 mbox_update_status(mb);
92 static void mbox_push(BCM2835Mbox *mb, uint32_t val)
94 assert(mb->count < MBOX_SIZE);
95 mb->reg[mb->count++] = val;
96 mbox_update_status(mb);
99 static void bcm2835_mbox_update(BCM2835MboxState *s)
106 /* Avoid unwanted recursive calls */
108 >>>>>>> upstreaming-raspi
109 s->mbox_irq_disabled = true;
111 /* Get pending responses and put them in the vc->arm mbox,
112 * as long as it's not full */
113 for (n = 0; n < MBOX_CHAN_COUNT; n++) {
114 while (s->available[n] && !(s->mbox[0].status & ARM_MS_FULL)) {
115 value = ldl_phys(&s->mbox_as, n << MBOX_AS_CHAN_SHIFT);
117 if (value == MBOX_INVALID_DATA) {
118 /* Interrupt pending, but there's no data. Hmmm... */
119 hw_error("%s: spurious interrupt on channel %d", __func__, n);
122 assert(value != MBOX_INVALID_DATA); /* Pending interrupt but no data */
123 >>>>>>> upstreaming-raspi
124 mbox_push(&s->mbox[0], value);
128 /* Try to push pending requests from the arm->vc mbox */
131 /* Re-enable calls from the IRQ routine */
132 s->mbox_irq_disabled = false;
134 /* Update ARM IRQ status */
136 s->mbox[0].config &= ~ARM_MC_IHAVEDATAIRQPEND;
137 if (!(s->mbox[0].status & ARM_MS_EMPTY)) {
138 s->mbox[0].config |= ARM_MC_IHAVEDATAIRQPEND;
139 if (s->mbox[0].config & ARM_MC_IHAVEDATAIRQEN) {
143 qemu_set_irq(s->arm_irq, set);
146 static void bcm2835_mbox_set_irq(void *opaque, int irq, int level)
148 BCM2835MboxState *s = opaque;
150 s->available[irq] = level;
152 /* avoid recursively calling bcm2835_mbox_update when the interrupt
154 * status changes due to the ldl_phys call within that function */
156 * status changes due to the ldl_phys call within that function
158 >>>>>>> upstreaming-raspi
159 if (!s->mbox_irq_disabled) {
160 bcm2835_mbox_update(s);
164 static uint64_t bcm2835_mbox_read(void *opaque, hwaddr offset, unsigned size)
166 BCM2835MboxState *s = opaque;
173 case 0x80 ... 0x8c: /* MAIL0_READ */
175 case 0x80 ... 0x8c: /* MAIL0_READ */
176 >>>>>>> upstreaming-raspi
177 if (s->mbox[0].status & ARM_MS_EMPTY) {
178 res = MBOX_INVALID_DATA;
180 res = mbox_pull(&s->mbox[0], 0);
184 case 0x90: /* MAIL0_PEEK */
185 res = s->mbox[0].reg[0];
187 case 0x94: /* MAIL0_SENDER */
189 case 0x98: /* MAIL0_STATUS */
190 res = s->mbox[0].status;
192 case 0x9c: /* MAIL0_CONFIG */
193 res = s->mbox[0].config;
195 case 0xb8: /* MAIL1_STATUS */
196 res = s->mbox[1].status;
201 res = s->mbox[0].reg[0];
208 res = s->mbox[0].status;
212 res = s->mbox[0].config;
216 res = s->mbox[1].status;
219 >>>>>>> upstreaming-raspi
221 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
226 bcm2835_mbox_update(s);
231 static void bcm2835_mbox_write(void *opaque, hwaddr offset,
232 uint64_t value, unsigned size)
234 BCM2835MboxState *s = opaque;
242 case 0x94: /* MAIL0_SENDER */
245 case 0x9c: /* MAIL0_CONFIG */
251 >>>>>>> upstreaming-raspi
252 s->mbox[0].config &= ~ARM_MC_IHAVEDATAIRQEN;
253 s->mbox[0].config |= value & ARM_MC_IHAVEDATAIRQEN;
259 case 0xa0 ... 0xac: /* MAIL1_WRITE */
260 >>>>>>> upstreaming-raspi
261 if (s->mbox[1].status & ARM_MS_FULL) {
263 qemu_log_mask(LOG_GUEST_ERROR, "%s: mailbox full\n", __func__);
266 if (ch < MBOX_CHAN_COUNT) {
267 childaddr = ch << MBOX_AS_CHAN_SHIFT;
268 if (ldl_phys(&s->mbox_as, childaddr + MBOX_AS_PENDING)) {
269 /* Child busy, push delayed. Push it in the arm->vc mbox */
270 mbox_push(&s->mbox[1], value);
272 /* Push it directly to the child device */
273 stl_phys(&s->mbox_as, childaddr, value);
276 /* Invalid channel number */
277 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid channel %u\n",
284 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
289 bcm2835_mbox_update(s);
292 static const MemoryRegionOps bcm2835_mbox_ops = {
293 .read = bcm2835_mbox_read,
294 .write = bcm2835_mbox_write,
295 .endianness = DEVICE_NATIVE_ENDIAN,
296 .valid.min_access_size = 4,
297 .valid.max_access_size = 4,
300 /* vmstate of a single mailbox */
301 static const VMStateDescription vmstate_bcm2835_mbox_box = {
302 .name = TYPE_BCM2835_MBOX "_box",
304 .minimum_version_id = 1,
305 .fields = (VMStateField[]) {
306 VMSTATE_UINT32_ARRAY(reg, BCM2835Mbox, MBOX_SIZE),
307 VMSTATE_UINT32(count, BCM2835Mbox),
308 VMSTATE_UINT32(status, BCM2835Mbox),
309 VMSTATE_UINT32(config, BCM2835Mbox),
310 VMSTATE_END_OF_LIST()
314 /* vmstate of the entire device */
315 static const VMStateDescription vmstate_bcm2835_mbox = {
316 .name = TYPE_BCM2835_MBOX,
318 .minimum_version_id = 1,
319 .minimum_version_id_old = 1,
320 .fields = (VMStateField[]) {
321 VMSTATE_BOOL_ARRAY(available, BCM2835MboxState, MBOX_CHAN_COUNT),
322 VMSTATE_STRUCT_ARRAY(mbox, BCM2835MboxState, 2, 1,
323 vmstate_bcm2835_mbox_box, BCM2835Mbox),
324 VMSTATE_END_OF_LIST()
328 static void bcm2835_mbox_init(Object *obj)
330 BCM2835MboxState *s = BCM2835_MBOX(obj);
334 >>>>>>> upstreaming-raspi
335 memory_region_init_io(&s->iomem, obj, &bcm2835_mbox_ops, s,
336 TYPE_BCM2835_MBOX, 0x400);
337 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
338 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->arm_irq);
339 qdev_init_gpio_in(DEVICE(s), bcm2835_mbox_set_irq, MBOX_CHAN_COUNT);
342 static void bcm2835_mbox_reset(DeviceState *dev)
344 BCM2835MboxState *s = BCM2835_MBOX(dev);
348 mbox_init(&s->mbox[0]);
349 mbox_init(&s->mbox[1]);
351 mbox_reset(&s->mbox[0]);
352 mbox_reset(&s->mbox[1]);
353 >>>>>>> upstreaming-raspi
354 s->mbox_irq_disabled = false;
355 for (n = 0; n < MBOX_CHAN_COUNT; n++) {
356 s->available[n] = false;
360 static void bcm2835_mbox_realize(DeviceState *dev, Error **errp)
362 BCM2835MboxState *s = BCM2835_MBOX(dev);
367 obj = object_property_get_link(OBJECT(dev), "mbox_mr", &err);
369 error_setg(errp, "%s: required mbox_mr link not found: %s",
371 obj = object_property_get_link(OBJECT(dev), "mbox-mr", &err);
373 error_setg(errp, "%s: required mbox-mr link not found: %s",
374 >>>>>>> upstreaming-raspi
375 __func__, error_get_pretty(err));
379 s->mbox_mr = MEMORY_REGION(obj);
380 address_space_init(&s->mbox_as, s->mbox_mr, NULL);
381 bcm2835_mbox_reset(dev);
384 static void bcm2835_mbox_class_init(ObjectClass *klass, void *data)
386 DeviceClass *dc = DEVICE_CLASS(klass);
388 dc->realize = bcm2835_mbox_realize;
389 dc->reset = bcm2835_mbox_reset;
390 dc->vmsd = &vmstate_bcm2835_mbox;
393 static TypeInfo bcm2835_mbox_info = {
394 .name = TYPE_BCM2835_MBOX,
395 .parent = TYPE_SYS_BUS_DEVICE,
396 .instance_size = sizeof(BCM2835MboxState),
397 .class_init = bcm2835_mbox_class_init,
398 .instance_init = bcm2835_mbox_init,
401 static void bcm2835_mbox_register_types(void)
403 type_register_static(&bcm2835_mbox_info);
406 type_init(bcm2835_mbox_register_types)