1cc28296dSAndrew Baumann /* 2cc28296dSAndrew Baumann * Rasperry Pi 2 emulation ARM control logic module. 3cc28296dSAndrew Baumann * Copyright (c) 2015, Microsoft 4cc28296dSAndrew Baumann * Written by Andrew Baumann 5cc28296dSAndrew Baumann * 6cc28296dSAndrew Baumann * Based on bcm2835_ic.c (Raspberry Pi emulation) (c) 2012 Gregory Estrade 7cc28296dSAndrew Baumann * This code is licensed under the GNU GPLv2 and later. 8cc28296dSAndrew Baumann * 9cc28296dSAndrew Baumann * At present, only implements interrupt routing, and mailboxes (i.e., 10cc28296dSAndrew Baumann * not local timer, PMU interrupt, or AXI counters). 11cc28296dSAndrew Baumann * 12cc28296dSAndrew Baumann * Ref: 13cc28296dSAndrew Baumann * https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2836/QA7_rev3.4.pdf 14cc28296dSAndrew Baumann */ 15cc28296dSAndrew Baumann 16c964b660SPeter Maydell #include "qemu/osdep.h" 17cc28296dSAndrew Baumann #include "hw/intc/bcm2836_control.h" 18*03dd024fSPaolo Bonzini #include "qemu/log.h" 19cc28296dSAndrew Baumann 20cc28296dSAndrew Baumann #define REG_GPU_ROUTE 0x0c 21cc28296dSAndrew Baumann #define REG_TIMERCONTROL 0x40 22cc28296dSAndrew Baumann #define REG_MBOXCONTROL 0x50 23cc28296dSAndrew Baumann #define REG_IRQSRC 0x60 24cc28296dSAndrew Baumann #define REG_FIQSRC 0x70 25cc28296dSAndrew Baumann #define REG_MBOX0_WR 0x80 26cc28296dSAndrew Baumann #define REG_MBOX0_RDCLR 0xc0 27cc28296dSAndrew Baumann #define REG_LIMIT 0x100 28cc28296dSAndrew Baumann 29cc28296dSAndrew Baumann #define IRQ_BIT(cntrl, num) (((cntrl) & (1 << (num))) != 0) 30cc28296dSAndrew Baumann #define FIQ_BIT(cntrl, num) (((cntrl) & (1 << ((num) + 4))) != 0) 31cc28296dSAndrew Baumann 32cc28296dSAndrew Baumann #define IRQ_CNTPSIRQ 0 33cc28296dSAndrew Baumann #define IRQ_CNTPNSIRQ 1 34cc28296dSAndrew Baumann #define IRQ_CNTHPIRQ 2 35cc28296dSAndrew Baumann #define IRQ_CNTVIRQ 3 36cc28296dSAndrew Baumann #define IRQ_MAILBOX0 4 37cc28296dSAndrew Baumann #define IRQ_MAILBOX1 5 38cc28296dSAndrew Baumann #define IRQ_MAILBOX2 6 39cc28296dSAndrew Baumann #define IRQ_MAILBOX3 7 40cc28296dSAndrew Baumann #define IRQ_GPU 8 41cc28296dSAndrew Baumann #define IRQ_PMU 9 42cc28296dSAndrew Baumann #define IRQ_AXI 10 43cc28296dSAndrew Baumann #define IRQ_TIMER 11 44cc28296dSAndrew Baumann #define IRQ_MAX IRQ_TIMER 45cc28296dSAndrew Baumann 46cc28296dSAndrew Baumann static void deliver_local(BCM2836ControlState *s, uint8_t core, uint8_t irq, 47cc28296dSAndrew Baumann uint32_t controlreg, uint8_t controlidx) 48cc28296dSAndrew Baumann { 49cc28296dSAndrew Baumann if (FIQ_BIT(controlreg, controlidx)) { 50cc28296dSAndrew Baumann /* deliver a FIQ */ 51cc28296dSAndrew Baumann s->fiqsrc[core] |= (uint32_t)1 << irq; 52cc28296dSAndrew Baumann } else if (IRQ_BIT(controlreg, controlidx)) { 53cc28296dSAndrew Baumann /* deliver an IRQ */ 54cc28296dSAndrew Baumann s->irqsrc[core] |= (uint32_t)1 << irq; 55cc28296dSAndrew Baumann } else { 56cc28296dSAndrew Baumann /* the interrupt is masked */ 57cc28296dSAndrew Baumann } 58cc28296dSAndrew Baumann } 59cc28296dSAndrew Baumann 60cc28296dSAndrew Baumann /* Update interrupts. */ 61cc28296dSAndrew Baumann static void bcm2836_control_update(BCM2836ControlState *s) 62cc28296dSAndrew Baumann { 63cc28296dSAndrew Baumann int i, j; 64cc28296dSAndrew Baumann 65cc28296dSAndrew Baumann /* reset pending IRQs/FIQs */ 66cc28296dSAndrew Baumann for (i = 0; i < BCM2836_NCORES; i++) { 67cc28296dSAndrew Baumann s->irqsrc[i] = s->fiqsrc[i] = 0; 68cc28296dSAndrew Baumann } 69cc28296dSAndrew Baumann 70cc28296dSAndrew Baumann /* apply routing logic, update status regs */ 71cc28296dSAndrew Baumann if (s->gpu_irq) { 72cc28296dSAndrew Baumann assert(s->route_gpu_irq < BCM2836_NCORES); 73cc28296dSAndrew Baumann s->irqsrc[s->route_gpu_irq] |= (uint32_t)1 << IRQ_GPU; 74cc28296dSAndrew Baumann } 75cc28296dSAndrew Baumann 76cc28296dSAndrew Baumann if (s->gpu_fiq) { 77cc28296dSAndrew Baumann assert(s->route_gpu_fiq < BCM2836_NCORES); 78cc28296dSAndrew Baumann s->fiqsrc[s->route_gpu_fiq] |= (uint32_t)1 << IRQ_GPU; 79cc28296dSAndrew Baumann } 80cc28296dSAndrew Baumann 81cc28296dSAndrew Baumann for (i = 0; i < BCM2836_NCORES; i++) { 82cc28296dSAndrew Baumann /* handle local timer interrupts for this core */ 83cc28296dSAndrew Baumann if (s->timerirqs[i]) { 84cc28296dSAndrew Baumann assert(s->timerirqs[i] < (1 << (IRQ_CNTVIRQ + 1))); /* sane mask? */ 85cc28296dSAndrew Baumann for (j = 0; j <= IRQ_CNTVIRQ; j++) { 86cc28296dSAndrew Baumann if ((s->timerirqs[i] & (1 << j)) != 0) { 87cc28296dSAndrew Baumann /* local interrupt j is set */ 88cc28296dSAndrew Baumann deliver_local(s, i, j, s->timercontrol[i], j); 89cc28296dSAndrew Baumann } 90cc28296dSAndrew Baumann } 91cc28296dSAndrew Baumann } 92cc28296dSAndrew Baumann 93cc28296dSAndrew Baumann /* handle mailboxes for this core */ 94cc28296dSAndrew Baumann for (j = 0; j < BCM2836_MBPERCORE; j++) { 95cc28296dSAndrew Baumann if (s->mailboxes[i * BCM2836_MBPERCORE + j] != 0) { 96cc28296dSAndrew Baumann /* mailbox j is set */ 97cc28296dSAndrew Baumann deliver_local(s, i, j + IRQ_MAILBOX0, s->mailboxcontrol[i], j); 98cc28296dSAndrew Baumann } 99cc28296dSAndrew Baumann } 100cc28296dSAndrew Baumann } 101cc28296dSAndrew Baumann 102cc28296dSAndrew Baumann /* call set_irq appropriately for each output */ 103cc28296dSAndrew Baumann for (i = 0; i < BCM2836_NCORES; i++) { 104cc28296dSAndrew Baumann qemu_set_irq(s->irq[i], s->irqsrc[i] != 0); 105cc28296dSAndrew Baumann qemu_set_irq(s->fiq[i], s->fiqsrc[i] != 0); 106cc28296dSAndrew Baumann } 107cc28296dSAndrew Baumann } 108cc28296dSAndrew Baumann 109cc28296dSAndrew Baumann static void bcm2836_control_set_local_irq(void *opaque, int core, int local_irq, 110cc28296dSAndrew Baumann int level) 111cc28296dSAndrew Baumann { 112cc28296dSAndrew Baumann BCM2836ControlState *s = opaque; 113cc28296dSAndrew Baumann 114cc28296dSAndrew Baumann assert(core >= 0 && core < BCM2836_NCORES); 115cc28296dSAndrew Baumann assert(local_irq >= 0 && local_irq <= IRQ_CNTVIRQ); 116cc28296dSAndrew Baumann 117cc28296dSAndrew Baumann s->timerirqs[core] = deposit32(s->timerirqs[core], local_irq, 1, !!level); 118cc28296dSAndrew Baumann 119cc28296dSAndrew Baumann bcm2836_control_update(s); 120cc28296dSAndrew Baumann } 121cc28296dSAndrew Baumann 122cc28296dSAndrew Baumann /* XXX: the following wrapper functions are a kludgy workaround, 123cc28296dSAndrew Baumann * needed because I can't seem to pass useful information in the "irq" 124cc28296dSAndrew Baumann * parameter when using named interrupts. Feel free to clean this up! 125cc28296dSAndrew Baumann */ 126cc28296dSAndrew Baumann 127cc28296dSAndrew Baumann static void bcm2836_control_set_local_irq0(void *opaque, int core, int level) 128cc28296dSAndrew Baumann { 129cc28296dSAndrew Baumann bcm2836_control_set_local_irq(opaque, core, 0, level); 130cc28296dSAndrew Baumann } 131cc28296dSAndrew Baumann 132cc28296dSAndrew Baumann static void bcm2836_control_set_local_irq1(void *opaque, int core, int level) 133cc28296dSAndrew Baumann { 134cc28296dSAndrew Baumann bcm2836_control_set_local_irq(opaque, core, 1, level); 135cc28296dSAndrew Baumann } 136cc28296dSAndrew Baumann 137cc28296dSAndrew Baumann static void bcm2836_control_set_local_irq2(void *opaque, int core, int level) 138cc28296dSAndrew Baumann { 139cc28296dSAndrew Baumann bcm2836_control_set_local_irq(opaque, core, 2, level); 140cc28296dSAndrew Baumann } 141cc28296dSAndrew Baumann 142cc28296dSAndrew Baumann static void bcm2836_control_set_local_irq3(void *opaque, int core, int level) 143cc28296dSAndrew Baumann { 144cc28296dSAndrew Baumann bcm2836_control_set_local_irq(opaque, core, 3, level); 145cc28296dSAndrew Baumann } 146cc28296dSAndrew Baumann 147cc28296dSAndrew Baumann static void bcm2836_control_set_gpu_irq(void *opaque, int irq, int level) 148cc28296dSAndrew Baumann { 149cc28296dSAndrew Baumann BCM2836ControlState *s = opaque; 150cc28296dSAndrew Baumann 151cc28296dSAndrew Baumann s->gpu_irq = level; 152cc28296dSAndrew Baumann 153cc28296dSAndrew Baumann bcm2836_control_update(s); 154cc28296dSAndrew Baumann } 155cc28296dSAndrew Baumann 156cc28296dSAndrew Baumann static void bcm2836_control_set_gpu_fiq(void *opaque, int irq, int level) 157cc28296dSAndrew Baumann { 158cc28296dSAndrew Baumann BCM2836ControlState *s = opaque; 159cc28296dSAndrew Baumann 160cc28296dSAndrew Baumann s->gpu_fiq = level; 161cc28296dSAndrew Baumann 162cc28296dSAndrew Baumann bcm2836_control_update(s); 163cc28296dSAndrew Baumann } 164cc28296dSAndrew Baumann 165cc28296dSAndrew Baumann static uint64_t bcm2836_control_read(void *opaque, hwaddr offset, unsigned size) 166cc28296dSAndrew Baumann { 167cc28296dSAndrew Baumann BCM2836ControlState *s = opaque; 168cc28296dSAndrew Baumann 169cc28296dSAndrew Baumann if (offset == REG_GPU_ROUTE) { 170cc28296dSAndrew Baumann assert(s->route_gpu_fiq < BCM2836_NCORES 171cc28296dSAndrew Baumann && s->route_gpu_irq < BCM2836_NCORES); 172cc28296dSAndrew Baumann return ((uint32_t)s->route_gpu_fiq << 2) | s->route_gpu_irq; 173cc28296dSAndrew Baumann } else if (offset >= REG_TIMERCONTROL && offset < REG_MBOXCONTROL) { 174cc28296dSAndrew Baumann return s->timercontrol[(offset - REG_TIMERCONTROL) >> 2]; 175cc28296dSAndrew Baumann } else if (offset >= REG_MBOXCONTROL && offset < REG_IRQSRC) { 176cc28296dSAndrew Baumann return s->mailboxcontrol[(offset - REG_MBOXCONTROL) >> 2]; 177cc28296dSAndrew Baumann } else if (offset >= REG_IRQSRC && offset < REG_FIQSRC) { 178cc28296dSAndrew Baumann return s->irqsrc[(offset - REG_IRQSRC) >> 2]; 179cc28296dSAndrew Baumann } else if (offset >= REG_FIQSRC && offset < REG_MBOX0_WR) { 180cc28296dSAndrew Baumann return s->fiqsrc[(offset - REG_FIQSRC) >> 2]; 181cc28296dSAndrew Baumann } else if (offset >= REG_MBOX0_RDCLR && offset < REG_LIMIT) { 182cc28296dSAndrew Baumann return s->mailboxes[(offset - REG_MBOX0_RDCLR) >> 2]; 183cc28296dSAndrew Baumann } else { 184cc28296dSAndrew Baumann qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n", 185cc28296dSAndrew Baumann __func__, offset); 186cc28296dSAndrew Baumann return 0; 187cc28296dSAndrew Baumann } 188cc28296dSAndrew Baumann } 189cc28296dSAndrew Baumann 190cc28296dSAndrew Baumann static void bcm2836_control_write(void *opaque, hwaddr offset, 191cc28296dSAndrew Baumann uint64_t val, unsigned size) 192cc28296dSAndrew Baumann { 193cc28296dSAndrew Baumann BCM2836ControlState *s = opaque; 194cc28296dSAndrew Baumann 195cc28296dSAndrew Baumann if (offset == REG_GPU_ROUTE) { 196cc28296dSAndrew Baumann s->route_gpu_irq = val & 0x3; 197cc28296dSAndrew Baumann s->route_gpu_fiq = (val >> 2) & 0x3; 198cc28296dSAndrew Baumann } else if (offset >= REG_TIMERCONTROL && offset < REG_MBOXCONTROL) { 199cc28296dSAndrew Baumann s->timercontrol[(offset - REG_TIMERCONTROL) >> 2] = val & 0xff; 200cc28296dSAndrew Baumann } else if (offset >= REG_MBOXCONTROL && offset < REG_IRQSRC) { 201cc28296dSAndrew Baumann s->mailboxcontrol[(offset - REG_MBOXCONTROL) >> 2] = val & 0xff; 202cc28296dSAndrew Baumann } else if (offset >= REG_MBOX0_WR && offset < REG_MBOX0_RDCLR) { 203cc28296dSAndrew Baumann s->mailboxes[(offset - REG_MBOX0_WR) >> 2] |= val; 204cc28296dSAndrew Baumann } else if (offset >= REG_MBOX0_RDCLR && offset < REG_LIMIT) { 205cc28296dSAndrew Baumann s->mailboxes[(offset - REG_MBOX0_RDCLR) >> 2] &= ~val; 206cc28296dSAndrew Baumann } else { 207cc28296dSAndrew Baumann qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n", 208cc28296dSAndrew Baumann __func__, offset); 209cc28296dSAndrew Baumann return; 210cc28296dSAndrew Baumann } 211cc28296dSAndrew Baumann 212cc28296dSAndrew Baumann bcm2836_control_update(s); 213cc28296dSAndrew Baumann } 214cc28296dSAndrew Baumann 215cc28296dSAndrew Baumann static const MemoryRegionOps bcm2836_control_ops = { 216cc28296dSAndrew Baumann .read = bcm2836_control_read, 217cc28296dSAndrew Baumann .write = bcm2836_control_write, 218cc28296dSAndrew Baumann .endianness = DEVICE_NATIVE_ENDIAN, 219cc28296dSAndrew Baumann .valid.min_access_size = 4, 220cc28296dSAndrew Baumann .valid.max_access_size = 4, 221cc28296dSAndrew Baumann }; 222cc28296dSAndrew Baumann 223cc28296dSAndrew Baumann static void bcm2836_control_reset(DeviceState *d) 224cc28296dSAndrew Baumann { 225cc28296dSAndrew Baumann BCM2836ControlState *s = BCM2836_CONTROL(d); 226cc28296dSAndrew Baumann int i; 227cc28296dSAndrew Baumann 228cc28296dSAndrew Baumann s->route_gpu_irq = s->route_gpu_fiq = 0; 229cc28296dSAndrew Baumann 230cc28296dSAndrew Baumann for (i = 0; i < BCM2836_NCORES; i++) { 231cc28296dSAndrew Baumann s->timercontrol[i] = 0; 232cc28296dSAndrew Baumann s->mailboxcontrol[i] = 0; 233cc28296dSAndrew Baumann } 234cc28296dSAndrew Baumann 235cc28296dSAndrew Baumann for (i = 0; i < BCM2836_NCORES * BCM2836_MBPERCORE; i++) { 236cc28296dSAndrew Baumann s->mailboxes[i] = 0; 237cc28296dSAndrew Baumann } 238cc28296dSAndrew Baumann } 239cc28296dSAndrew Baumann 240cc28296dSAndrew Baumann static void bcm2836_control_init(Object *obj) 241cc28296dSAndrew Baumann { 242cc28296dSAndrew Baumann BCM2836ControlState *s = BCM2836_CONTROL(obj); 243cc28296dSAndrew Baumann DeviceState *dev = DEVICE(obj); 244cc28296dSAndrew Baumann 245cc28296dSAndrew Baumann memory_region_init_io(&s->iomem, obj, &bcm2836_control_ops, s, 246cc28296dSAndrew Baumann TYPE_BCM2836_CONTROL, REG_LIMIT); 247cc28296dSAndrew Baumann sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem); 248cc28296dSAndrew Baumann 249cc28296dSAndrew Baumann /* inputs from each CPU core */ 250cc28296dSAndrew Baumann qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq0, "cntpsirq", 251cc28296dSAndrew Baumann BCM2836_NCORES); 252cc28296dSAndrew Baumann qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq1, "cntpnsirq", 253cc28296dSAndrew Baumann BCM2836_NCORES); 254cc28296dSAndrew Baumann qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq2, "cnthpirq", 255cc28296dSAndrew Baumann BCM2836_NCORES); 256cc28296dSAndrew Baumann qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq3, "cntvirq", 257cc28296dSAndrew Baumann BCM2836_NCORES); 258cc28296dSAndrew Baumann 259cc28296dSAndrew Baumann /* IRQ and FIQ inputs from upstream bcm2835 controller */ 260cc28296dSAndrew Baumann qdev_init_gpio_in_named(dev, bcm2836_control_set_gpu_irq, "gpu-irq", 1); 261cc28296dSAndrew Baumann qdev_init_gpio_in_named(dev, bcm2836_control_set_gpu_fiq, "gpu-fiq", 1); 262cc28296dSAndrew Baumann 263cc28296dSAndrew Baumann /* outputs to CPU cores */ 264cc28296dSAndrew Baumann qdev_init_gpio_out_named(dev, s->irq, "irq", BCM2836_NCORES); 265cc28296dSAndrew Baumann qdev_init_gpio_out_named(dev, s->fiq, "fiq", BCM2836_NCORES); 266cc28296dSAndrew Baumann } 267cc28296dSAndrew Baumann 268cc28296dSAndrew Baumann static const VMStateDescription vmstate_bcm2836_control = { 269cc28296dSAndrew Baumann .name = TYPE_BCM2836_CONTROL, 270cc28296dSAndrew Baumann .version_id = 1, 271cc28296dSAndrew Baumann .minimum_version_id = 1, 272cc28296dSAndrew Baumann .fields = (VMStateField[]) { 273cc28296dSAndrew Baumann VMSTATE_UINT32_ARRAY(mailboxes, BCM2836ControlState, 274cc28296dSAndrew Baumann BCM2836_NCORES * BCM2836_MBPERCORE), 275cc28296dSAndrew Baumann VMSTATE_UINT8(route_gpu_irq, BCM2836ControlState), 276cc28296dSAndrew Baumann VMSTATE_UINT8(route_gpu_fiq, BCM2836ControlState), 277cc28296dSAndrew Baumann VMSTATE_UINT32_ARRAY(timercontrol, BCM2836ControlState, BCM2836_NCORES), 278cc28296dSAndrew Baumann VMSTATE_UINT32_ARRAY(mailboxcontrol, BCM2836ControlState, 279cc28296dSAndrew Baumann BCM2836_NCORES), 280cc28296dSAndrew Baumann VMSTATE_END_OF_LIST() 281cc28296dSAndrew Baumann } 282cc28296dSAndrew Baumann }; 283cc28296dSAndrew Baumann 284cc28296dSAndrew Baumann static void bcm2836_control_class_init(ObjectClass *klass, void *data) 285cc28296dSAndrew Baumann { 286cc28296dSAndrew Baumann DeviceClass *dc = DEVICE_CLASS(klass); 287cc28296dSAndrew Baumann 288cc28296dSAndrew Baumann dc->reset = bcm2836_control_reset; 289cc28296dSAndrew Baumann dc->vmsd = &vmstate_bcm2836_control; 290cc28296dSAndrew Baumann } 291cc28296dSAndrew Baumann 292cc28296dSAndrew Baumann static TypeInfo bcm2836_control_info = { 293cc28296dSAndrew Baumann .name = TYPE_BCM2836_CONTROL, 294cc28296dSAndrew Baumann .parent = TYPE_SYS_BUS_DEVICE, 295cc28296dSAndrew Baumann .instance_size = sizeof(BCM2836ControlState), 296cc28296dSAndrew Baumann .class_init = bcm2836_control_class_init, 297cc28296dSAndrew Baumann .instance_init = bcm2836_control_init, 298cc28296dSAndrew Baumann }; 299cc28296dSAndrew Baumann 300cc28296dSAndrew Baumann static void bcm2836_control_register_types(void) 301cc28296dSAndrew Baumann { 302cc28296dSAndrew Baumann type_register_static(&bcm2836_control_info); 303cc28296dSAndrew Baumann } 304cc28296dSAndrew Baumann 305cc28296dSAndrew Baumann type_init(bcm2836_control_register_types) 306