xref: /qemu/hw/intc/xilinx_intc.c (revision 1437c94b2689c2010362f84d14f14feaa1d8dba3)
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;
119fa96d614SPeter Crosthwaite         case R_ISR:
120fa96d614SPeter Crosthwaite             if ((p->regs[R_MER] & 2)) {
121fa96d614SPeter Crosthwaite                 break;
122fa96d614SPeter Crosthwaite             }
123fa96d614SPeter 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 
14645fdd3bfSPeter Crosthwaite     /* edge triggered interrupt */
14745fdd3bfSPeter Crosthwaite     if (p->c_kind_of_intr & (1 << irq) && p->regs[R_MER] & 2) {
14817628bc6SEdgar E. Iglesias         p->regs[R_ISR] |= (level << irq);
14945fdd3bfSPeter Crosthwaite     }
15045fdd3bfSPeter Crosthwaite 
15145fdd3bfSPeter Crosthwaite     p->irq_pin_state &= ~(1 << irq);
15245fdd3bfSPeter Crosthwaite     p->irq_pin_state |= level << irq;
15317628bc6SEdgar E. Iglesias     update_irq(p);
15417628bc6SEdgar E. Iglesias }
15517628bc6SEdgar E. Iglesias 
15681a322d4SGerd Hoffmann static int xilinx_intc_init(SysBusDevice *dev)
15717628bc6SEdgar E. Iglesias {
15817628bc6SEdgar E. Iglesias     struct xlx_pic *p = FROM_SYSBUS(typeof (*p), dev);
15917628bc6SEdgar E. Iglesias 
16017628bc6SEdgar E. Iglesias     qdev_init_gpio_in(&dev->qdev, irq_handler, 32);
16117628bc6SEdgar E. Iglesias     sysbus_init_irq(dev, &p->parent_irq);
16217628bc6SEdgar E. Iglesias 
163*1437c94bSPaolo Bonzini     memory_region_init_io(&p->mmio, OBJECT(p), &pic_ops, p, "xlnx.xps-intc",
164*1437c94bSPaolo Bonzini                           R_MAX * 4);
165750ecd44SAvi Kivity     sysbus_init_mmio(dev, &p->mmio);
16681a322d4SGerd Hoffmann     return 0;
16717628bc6SEdgar E. Iglesias }
16817628bc6SEdgar E. Iglesias 
169999e12bbSAnthony Liguori static Property xilinx_intc_properties[] = {
1708017dc26SGerd Hoffmann     DEFINE_PROP_UINT32("kind-of-intr", struct xlx_pic, c_kind_of_intr, 0),
1718017dc26SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
172999e12bbSAnthony Liguori };
173999e12bbSAnthony Liguori 
174999e12bbSAnthony Liguori static void xilinx_intc_class_init(ObjectClass *klass, void *data)
175999e12bbSAnthony Liguori {
17639bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
177999e12bbSAnthony Liguori     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
178999e12bbSAnthony Liguori 
179999e12bbSAnthony Liguori     k->init = xilinx_intc_init;
18039bffca2SAnthony Liguori     dc->props = xilinx_intc_properties;
181ee6847d1SGerd Hoffmann }
182999e12bbSAnthony Liguori 
1838c43a6f0SAndreas Färber static const TypeInfo xilinx_intc_info = {
18424739ab4SPeter A. G. Crosthwaite     .name          = "xlnx.xps-intc",
18539bffca2SAnthony Liguori     .parent        = TYPE_SYS_BUS_DEVICE,
18639bffca2SAnthony Liguori     .instance_size = sizeof(struct xlx_pic),
187999e12bbSAnthony Liguori     .class_init    = xilinx_intc_class_init,
188ee6847d1SGerd Hoffmann };
189ee6847d1SGerd Hoffmann 
19083f7d43aSAndreas Färber static void xilinx_intc_register_types(void)
19117628bc6SEdgar E. Iglesias {
19239bffca2SAnthony Liguori     type_register_static(&xilinx_intc_info);
19317628bc6SEdgar E. Iglesias }
19417628bc6SEdgar E. Iglesias 
19583f7d43aSAndreas Färber type_init(xilinx_intc_register_types)
196