xref: /qemu/hw/intc/xilinx_intc.c (revision fa96d6142f7f1947717c7c45c4d3141e5ab55167)
117628bc6SEdgar E. Iglesias /*
217628bc6SEdgar E. Iglesias  * QEMU Xilinx OPB Interrupt Controller.
317628bc6SEdgar E. Iglesias  *
417628bc6SEdgar E. Iglesias  * Copyright (c) 2009 Edgar E. Iglesias.
517628bc6SEdgar E. Iglesias  *
617628bc6SEdgar E. Iglesias  * Permission is hereby granted, free of charge, to any person obtaining a copy
717628bc6SEdgar E. Iglesias  * of this software and associated documentation files (the "Software"), to deal
817628bc6SEdgar E. Iglesias  * in the Software without restriction, including without limitation the rights
917628bc6SEdgar E. Iglesias  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1017628bc6SEdgar E. Iglesias  * copies of the Software, and to permit persons to whom the Software is
1117628bc6SEdgar E. Iglesias  * furnished to do so, subject to the following conditions:
1217628bc6SEdgar E. Iglesias  *
1317628bc6SEdgar E. Iglesias  * The above copyright notice and this permission notice shall be included in
1417628bc6SEdgar E. Iglesias  * all copies or substantial portions of the Software.
1517628bc6SEdgar E. Iglesias  *
1617628bc6SEdgar E. Iglesias  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717628bc6SEdgar E. Iglesias  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1817628bc6SEdgar E. Iglesias  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1917628bc6SEdgar E. Iglesias  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2017628bc6SEdgar E. Iglesias  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2117628bc6SEdgar E. Iglesias  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2217628bc6SEdgar E. Iglesias  * THE SOFTWARE.
2317628bc6SEdgar E. Iglesias  */
2417628bc6SEdgar E. Iglesias 
2583c9f4caSPaolo Bonzini #include "hw/sysbus.h"
2683c9f4caSPaolo Bonzini #include "hw/hw.h"
2717628bc6SEdgar E. Iglesias 
2817628bc6SEdgar E. Iglesias #define D(x)
2917628bc6SEdgar E. Iglesias 
3017628bc6SEdgar E. Iglesias #define R_ISR       0
3117628bc6SEdgar E. Iglesias #define R_IPR       1
3217628bc6SEdgar E. Iglesias #define R_IER       2
3317628bc6SEdgar E. Iglesias #define R_IAR       3
3417628bc6SEdgar E. Iglesias #define R_SIE       4
3517628bc6SEdgar E. Iglesias #define R_CIE       5
3617628bc6SEdgar E. Iglesias #define R_IVR       6
3717628bc6SEdgar E. Iglesias #define R_MER       7
3817628bc6SEdgar E. Iglesias #define R_MAX       8
3917628bc6SEdgar E. Iglesias 
4017628bc6SEdgar E. Iglesias struct xlx_pic
4117628bc6SEdgar E. Iglesias {
4217628bc6SEdgar E. Iglesias     SysBusDevice busdev;
43010f3f5fSEdgar E. Iglesias     MemoryRegion mmio;
4417628bc6SEdgar E. Iglesias     qemu_irq parent_irq;
4517628bc6SEdgar E. Iglesias 
4617628bc6SEdgar E. Iglesias     /* Configuration reg chosen at synthesis-time. QEMU populates
4717628bc6SEdgar E. Iglesias        the bits at board-setup.  */
4817628bc6SEdgar E. Iglesias     uint32_t c_kind_of_intr;
4917628bc6SEdgar E. Iglesias 
5017628bc6SEdgar E. Iglesias     /* Runtime control registers.  */
5117628bc6SEdgar E. Iglesias     uint32_t regs[R_MAX];
5245fdd3bfSPeter Crosthwaite     /* state of the interrupt input pins */
5345fdd3bfSPeter Crosthwaite     uint32_t irq_pin_state;
5417628bc6SEdgar E. Iglesias };
5517628bc6SEdgar E. Iglesias 
5617628bc6SEdgar E. Iglesias static void update_irq(struct xlx_pic *p)
5717628bc6SEdgar E. Iglesias {
5817628bc6SEdgar E. Iglesias     uint32_t i;
5945fdd3bfSPeter Crosthwaite 
6045fdd3bfSPeter Crosthwaite     /* level triggered interrupt */
6145fdd3bfSPeter Crosthwaite     if (p->regs[R_MER] & 2) {
6245fdd3bfSPeter Crosthwaite         p->regs[R_ISR] |= p->irq_pin_state & ~p->c_kind_of_intr;
6345fdd3bfSPeter Crosthwaite     }
6445fdd3bfSPeter Crosthwaite 
6517628bc6SEdgar E. Iglesias     /* Update the pending register.  */
6617628bc6SEdgar E. Iglesias     p->regs[R_IPR] = p->regs[R_ISR] & p->regs[R_IER];
6717628bc6SEdgar E. Iglesias 
6817628bc6SEdgar E. Iglesias     /* Update the vector register.  */
6917628bc6SEdgar E. Iglesias     for (i = 0; i < 32; i++) {
7017628bc6SEdgar E. Iglesias         if (p->regs[R_IPR] & (1 << i))
7117628bc6SEdgar E. Iglesias             break;
7217628bc6SEdgar E. Iglesias     }
7317628bc6SEdgar E. Iglesias     if (i == 32)
7417628bc6SEdgar E. Iglesias         i = ~0;
7517628bc6SEdgar E. Iglesias 
7617628bc6SEdgar E. Iglesias     p->regs[R_IVR] = i;
775c9f4336SPeter Crosthwaite     qemu_set_irq(p->parent_irq, (p->regs[R_MER] & 1) && p->regs[R_IPR]);
7817628bc6SEdgar E. Iglesias }
7917628bc6SEdgar E. Iglesias 
80010f3f5fSEdgar E. Iglesias static uint64_t
81a8170e5eSAvi Kivity pic_read(void *opaque, hwaddr addr, unsigned int size)
8217628bc6SEdgar E. Iglesias {
8317628bc6SEdgar E. Iglesias     struct xlx_pic *p = opaque;
8417628bc6SEdgar E. Iglesias     uint32_t r = 0;
8517628bc6SEdgar E. Iglesias 
8617628bc6SEdgar E. Iglesias     addr >>= 2;
8717628bc6SEdgar E. Iglesias     switch (addr)
8817628bc6SEdgar E. Iglesias     {
8917628bc6SEdgar E. Iglesias         default:
9017628bc6SEdgar E. Iglesias             if (addr < ARRAY_SIZE(p->regs))
9117628bc6SEdgar E. Iglesias                 r = p->regs[addr];
9217628bc6SEdgar E. Iglesias             break;
9317628bc6SEdgar E. Iglesias 
9417628bc6SEdgar E. Iglesias     }
9517628bc6SEdgar E. Iglesias     D(printf("%s %x=%x\n", __func__, addr * 4, r));
9617628bc6SEdgar E. Iglesias     return r;
9717628bc6SEdgar E. Iglesias }
9817628bc6SEdgar E. Iglesias 
9917628bc6SEdgar E. Iglesias static void
100a8170e5eSAvi Kivity pic_write(void *opaque, hwaddr addr,
101010f3f5fSEdgar E. Iglesias           uint64_t val64, unsigned int size)
10217628bc6SEdgar E. Iglesias {
10317628bc6SEdgar E. Iglesias     struct xlx_pic *p = opaque;
104010f3f5fSEdgar E. Iglesias     uint32_t value = val64;
10517628bc6SEdgar E. Iglesias 
10617628bc6SEdgar E. Iglesias     addr >>= 2;
10717628bc6SEdgar E. Iglesias     D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
10817628bc6SEdgar E. Iglesias     switch (addr)
10917628bc6SEdgar E. Iglesias     {
11017628bc6SEdgar E. Iglesias         case R_IAR:
11117628bc6SEdgar E. Iglesias             p->regs[R_ISR] &= ~value; /* ACK.  */
11217628bc6SEdgar E. Iglesias             break;
11317628bc6SEdgar E. Iglesias         case R_SIE:
11417628bc6SEdgar E. Iglesias             p->regs[R_IER] |= value;  /* Atomic set ie.  */
11517628bc6SEdgar E. Iglesias             break;
11617628bc6SEdgar E. Iglesias         case R_CIE:
11717628bc6SEdgar E. Iglesias             p->regs[R_IER] &= ~value; /* Atomic clear ie.  */
11817628bc6SEdgar E. Iglesias             break;
119*fa96d614SPeter Crosthwaite         case R_ISR:
120*fa96d614SPeter Crosthwaite             if ((p->regs[R_MER] & 2)) {
121*fa96d614SPeter Crosthwaite                 break;
122*fa96d614SPeter Crosthwaite             }
123*fa96d614SPeter Crosthwaite             /* fallthrough */
12417628bc6SEdgar E. Iglesias         default:
12517628bc6SEdgar E. Iglesias             if (addr < ARRAY_SIZE(p->regs))
12617628bc6SEdgar E. Iglesias                 p->regs[addr] = value;
12717628bc6SEdgar E. Iglesias             break;
12817628bc6SEdgar E. Iglesias     }
12917628bc6SEdgar E. Iglesias     update_irq(p);
13017628bc6SEdgar E. Iglesias }
13117628bc6SEdgar E. Iglesias 
132010f3f5fSEdgar E. Iglesias static const MemoryRegionOps pic_ops = {
133010f3f5fSEdgar E. Iglesias     .read = pic_read,
134010f3f5fSEdgar E. Iglesias     .write = pic_write,
135010f3f5fSEdgar E. Iglesias     .endianness = DEVICE_NATIVE_ENDIAN,
136010f3f5fSEdgar E. Iglesias     .valid = {
137010f3f5fSEdgar E. Iglesias         .min_access_size = 4,
138010f3f5fSEdgar E. Iglesias         .max_access_size = 4
139010f3f5fSEdgar E. Iglesias     }
14017628bc6SEdgar E. Iglesias };
14117628bc6SEdgar E. Iglesias 
14217628bc6SEdgar E. Iglesias static void irq_handler(void *opaque, int irq, int level)
14317628bc6SEdgar E. Iglesias {
14417628bc6SEdgar E. Iglesias     struct xlx_pic *p = opaque;
14517628bc6SEdgar E. Iglesias 
14617628bc6SEdgar E. Iglesias     if (!(p->regs[R_MER] & 2)) {
14717628bc6SEdgar E. Iglesias         qemu_irq_lower(p->parent_irq);
14817628bc6SEdgar E. Iglesias         return;
14917628bc6SEdgar E. Iglesias     }
15017628bc6SEdgar E. Iglesias 
15145fdd3bfSPeter Crosthwaite     /* edge triggered interrupt */
15245fdd3bfSPeter Crosthwaite     if (p->c_kind_of_intr & (1 << irq) && p->regs[R_MER] & 2) {
15317628bc6SEdgar E. Iglesias         p->regs[R_ISR] |= (level << irq);
15445fdd3bfSPeter Crosthwaite     }
15545fdd3bfSPeter Crosthwaite 
15645fdd3bfSPeter Crosthwaite     p->irq_pin_state &= ~(1 << irq);
15745fdd3bfSPeter Crosthwaite     p->irq_pin_state |= level << irq;
15817628bc6SEdgar E. Iglesias     update_irq(p);
15917628bc6SEdgar E. Iglesias }
16017628bc6SEdgar E. Iglesias 
16181a322d4SGerd Hoffmann static int xilinx_intc_init(SysBusDevice *dev)
16217628bc6SEdgar E. Iglesias {
16317628bc6SEdgar E. Iglesias     struct xlx_pic *p = FROM_SYSBUS(typeof (*p), dev);
16417628bc6SEdgar E. Iglesias 
16517628bc6SEdgar E. Iglesias     qdev_init_gpio_in(&dev->qdev, irq_handler, 32);
16617628bc6SEdgar E. Iglesias     sysbus_init_irq(dev, &p->parent_irq);
16717628bc6SEdgar E. Iglesias 
16824739ab4SPeter A. G. Crosthwaite     memory_region_init_io(&p->mmio, &pic_ops, p, "xlnx.xps-intc", R_MAX * 4);
169750ecd44SAvi Kivity     sysbus_init_mmio(dev, &p->mmio);
17081a322d4SGerd Hoffmann     return 0;
17117628bc6SEdgar E. Iglesias }
17217628bc6SEdgar E. Iglesias 
173999e12bbSAnthony Liguori static Property xilinx_intc_properties[] = {
1748017dc26SGerd Hoffmann     DEFINE_PROP_UINT32("kind-of-intr", struct xlx_pic, c_kind_of_intr, 0),
1758017dc26SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
176999e12bbSAnthony Liguori };
177999e12bbSAnthony Liguori 
178999e12bbSAnthony Liguori static void xilinx_intc_class_init(ObjectClass *klass, void *data)
179999e12bbSAnthony Liguori {
18039bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
181999e12bbSAnthony Liguori     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
182999e12bbSAnthony Liguori 
183999e12bbSAnthony Liguori     k->init = xilinx_intc_init;
18439bffca2SAnthony Liguori     dc->props = xilinx_intc_properties;
185ee6847d1SGerd Hoffmann }
186999e12bbSAnthony Liguori 
1878c43a6f0SAndreas Färber static const TypeInfo xilinx_intc_info = {
18824739ab4SPeter A. G. Crosthwaite     .name          = "xlnx.xps-intc",
18939bffca2SAnthony Liguori     .parent        = TYPE_SYS_BUS_DEVICE,
19039bffca2SAnthony Liguori     .instance_size = sizeof(struct xlx_pic),
191999e12bbSAnthony Liguori     .class_init    = xilinx_intc_class_init,
192ee6847d1SGerd Hoffmann };
193ee6847d1SGerd Hoffmann 
19483f7d43aSAndreas Färber static void xilinx_intc_register_types(void)
19517628bc6SEdgar E. Iglesias {
19639bffca2SAnthony Liguori     type_register_static(&xilinx_intc_info);
19717628bc6SEdgar E. Iglesias }
19817628bc6SEdgar E. Iglesias 
19983f7d43aSAndreas Färber type_init(xilinx_intc_register_types)
200