xref: /qemu/hw/intc/xilinx_intc.c (revision a373cdb5cebd58b2e3103e048894ba55910269f6)
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 
40cc3e064eSAndreas Färber #define TYPE_XILINX_INTC "xlnx.xps-intc"
41cc3e064eSAndreas Färber #define XILINX_INTC(obj) OBJECT_CHECK(struct xlx_pic, (obj), TYPE_XILINX_INTC)
42cc3e064eSAndreas Färber 
4317628bc6SEdgar E. Iglesias struct xlx_pic
4417628bc6SEdgar E. Iglesias {
45cc3e064eSAndreas Färber     SysBusDevice parent_obj;
46cc3e064eSAndreas Färber 
47010f3f5fSEdgar E. Iglesias     MemoryRegion mmio;
4817628bc6SEdgar E. Iglesias     qemu_irq parent_irq;
4917628bc6SEdgar E. Iglesias 
5017628bc6SEdgar E. Iglesias     /* Configuration reg chosen at synthesis-time. QEMU populates
5117628bc6SEdgar E. Iglesias        the bits at board-setup.  */
5217628bc6SEdgar E. Iglesias     uint32_t c_kind_of_intr;
5317628bc6SEdgar E. Iglesias 
5417628bc6SEdgar E. Iglesias     /* Runtime control registers.  */
5517628bc6SEdgar E. Iglesias     uint32_t regs[R_MAX];
5645fdd3bfSPeter Crosthwaite     /* state of the interrupt input pins */
5745fdd3bfSPeter Crosthwaite     uint32_t irq_pin_state;
5817628bc6SEdgar E. Iglesias };
5917628bc6SEdgar E. Iglesias 
6017628bc6SEdgar E. Iglesias static void update_irq(struct xlx_pic *p)
6117628bc6SEdgar E. Iglesias {
6217628bc6SEdgar E. Iglesias     uint32_t i;
6345fdd3bfSPeter Crosthwaite 
6445fdd3bfSPeter Crosthwaite     /* level triggered interrupt */
6545fdd3bfSPeter Crosthwaite     if (p->regs[R_MER] & 2) {
6645fdd3bfSPeter Crosthwaite         p->regs[R_ISR] |= p->irq_pin_state & ~p->c_kind_of_intr;
6745fdd3bfSPeter Crosthwaite     }
6845fdd3bfSPeter Crosthwaite 
6917628bc6SEdgar E. Iglesias     /* Update the pending register.  */
7017628bc6SEdgar E. Iglesias     p->regs[R_IPR] = p->regs[R_ISR] & p->regs[R_IER];
7117628bc6SEdgar E. Iglesias 
7217628bc6SEdgar E. Iglesias     /* Update the vector register.  */
7317628bc6SEdgar E. Iglesias     for (i = 0; i < 32; i++) {
740bc60bd7SPeter Maydell         if (p->regs[R_IPR] & (1U << i)) {
7517628bc6SEdgar E. Iglesias             break;
7617628bc6SEdgar E. Iglesias         }
770bc60bd7SPeter Maydell     }
7817628bc6SEdgar E. Iglesias     if (i == 32)
7917628bc6SEdgar E. Iglesias         i = ~0;
8017628bc6SEdgar E. Iglesias 
8117628bc6SEdgar E. Iglesias     p->regs[R_IVR] = i;
825c9f4336SPeter Crosthwaite     qemu_set_irq(p->parent_irq, (p->regs[R_MER] & 1) && p->regs[R_IPR]);
8317628bc6SEdgar E. Iglesias }
8417628bc6SEdgar E. Iglesias 
85010f3f5fSEdgar E. Iglesias static uint64_t
86a8170e5eSAvi Kivity pic_read(void *opaque, hwaddr addr, unsigned int size)
8717628bc6SEdgar E. Iglesias {
8817628bc6SEdgar E. Iglesias     struct xlx_pic *p = opaque;
8917628bc6SEdgar E. Iglesias     uint32_t r = 0;
9017628bc6SEdgar E. Iglesias 
9117628bc6SEdgar E. Iglesias     addr >>= 2;
9217628bc6SEdgar E. Iglesias     switch (addr)
9317628bc6SEdgar E. Iglesias     {
9417628bc6SEdgar E. Iglesias         default:
9517628bc6SEdgar E. Iglesias             if (addr < ARRAY_SIZE(p->regs))
9617628bc6SEdgar E. Iglesias                 r = p->regs[addr];
9717628bc6SEdgar E. Iglesias             break;
9817628bc6SEdgar E. Iglesias 
9917628bc6SEdgar E. Iglesias     }
10017628bc6SEdgar E. Iglesias     D(printf("%s %x=%x\n", __func__, addr * 4, r));
10117628bc6SEdgar E. Iglesias     return r;
10217628bc6SEdgar E. Iglesias }
10317628bc6SEdgar E. Iglesias 
10417628bc6SEdgar E. Iglesias static void
105a8170e5eSAvi Kivity pic_write(void *opaque, hwaddr addr,
106010f3f5fSEdgar E. Iglesias           uint64_t val64, unsigned int size)
10717628bc6SEdgar E. Iglesias {
10817628bc6SEdgar E. Iglesias     struct xlx_pic *p = opaque;
109010f3f5fSEdgar E. Iglesias     uint32_t value = val64;
11017628bc6SEdgar E. Iglesias 
11117628bc6SEdgar E. Iglesias     addr >>= 2;
11217628bc6SEdgar E. Iglesias     D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
11317628bc6SEdgar E. Iglesias     switch (addr)
11417628bc6SEdgar E. Iglesias     {
11517628bc6SEdgar E. Iglesias         case R_IAR:
11617628bc6SEdgar E. Iglesias             p->regs[R_ISR] &= ~value; /* ACK.  */
11717628bc6SEdgar E. Iglesias             break;
11817628bc6SEdgar E. Iglesias         case R_SIE:
11917628bc6SEdgar E. Iglesias             p->regs[R_IER] |= value;  /* Atomic set ie.  */
12017628bc6SEdgar E. Iglesias             break;
12117628bc6SEdgar E. Iglesias         case R_CIE:
12217628bc6SEdgar E. Iglesias             p->regs[R_IER] &= ~value; /* Atomic clear ie.  */
12317628bc6SEdgar E. Iglesias             break;
12412f7fb60SGuenter Roeck         case R_MER:
12512f7fb60SGuenter Roeck             p->regs[R_MER] = value & 0x3;
12612f7fb60SGuenter Roeck             break;
127fa96d614SPeter Crosthwaite         case R_ISR:
128fa96d614SPeter Crosthwaite             if ((p->regs[R_MER] & 2)) {
129fa96d614SPeter Crosthwaite                 break;
130fa96d614SPeter Crosthwaite             }
131fa96d614SPeter Crosthwaite             /* fallthrough */
13217628bc6SEdgar E. Iglesias         default:
13317628bc6SEdgar E. Iglesias             if (addr < ARRAY_SIZE(p->regs))
13417628bc6SEdgar E. Iglesias                 p->regs[addr] = value;
13517628bc6SEdgar E. Iglesias             break;
13617628bc6SEdgar E. Iglesias     }
13717628bc6SEdgar E. Iglesias     update_irq(p);
13817628bc6SEdgar E. Iglesias }
13917628bc6SEdgar E. Iglesias 
140010f3f5fSEdgar E. Iglesias static const MemoryRegionOps pic_ops = {
141010f3f5fSEdgar E. Iglesias     .read = pic_read,
142010f3f5fSEdgar E. Iglesias     .write = pic_write,
143010f3f5fSEdgar E. Iglesias     .endianness = DEVICE_NATIVE_ENDIAN,
144010f3f5fSEdgar E. Iglesias     .valid = {
145010f3f5fSEdgar E. Iglesias         .min_access_size = 4,
146010f3f5fSEdgar E. Iglesias         .max_access_size = 4
147010f3f5fSEdgar E. Iglesias     }
14817628bc6SEdgar E. Iglesias };
14917628bc6SEdgar E. Iglesias 
15017628bc6SEdgar E. Iglesias static void irq_handler(void *opaque, int irq, int level)
15117628bc6SEdgar E. Iglesias {
15217628bc6SEdgar E. Iglesias     struct xlx_pic *p = opaque;
15317628bc6SEdgar E. Iglesias 
15445fdd3bfSPeter Crosthwaite     /* edge triggered interrupt */
15545fdd3bfSPeter Crosthwaite     if (p->c_kind_of_intr & (1 << irq) && p->regs[R_MER] & 2) {
15617628bc6SEdgar E. Iglesias         p->regs[R_ISR] |= (level << irq);
15745fdd3bfSPeter Crosthwaite     }
15845fdd3bfSPeter Crosthwaite 
15945fdd3bfSPeter Crosthwaite     p->irq_pin_state &= ~(1 << irq);
16045fdd3bfSPeter Crosthwaite     p->irq_pin_state |= level << irq;
16117628bc6SEdgar E. Iglesias     update_irq(p);
16217628bc6SEdgar E. Iglesias }
16317628bc6SEdgar E. Iglesias 
164*a373cdb5SPeter Crosthwaite static void xilinx_intc_init(Object *obj)
16517628bc6SEdgar E. Iglesias {
166*a373cdb5SPeter Crosthwaite     struct xlx_pic *p = XILINX_INTC(obj);
16717628bc6SEdgar E. Iglesias 
168*a373cdb5SPeter Crosthwaite     qdev_init_gpio_in(DEVICE(obj), irq_handler, 32);
169*a373cdb5SPeter Crosthwaite     sysbus_init_irq(SYS_BUS_DEVICE(obj), &p->parent_irq);
17017628bc6SEdgar E. Iglesias 
171*a373cdb5SPeter Crosthwaite     memory_region_init_io(&p->mmio, obj, &pic_ops, p, "xlnx.xps-intc",
1721437c94bSPaolo Bonzini                           R_MAX * 4);
173*a373cdb5SPeter Crosthwaite     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &p->mmio);
17417628bc6SEdgar E. Iglesias }
17517628bc6SEdgar E. Iglesias 
176999e12bbSAnthony Liguori static Property xilinx_intc_properties[] = {
1778017dc26SGerd Hoffmann     DEFINE_PROP_UINT32("kind-of-intr", struct xlx_pic, c_kind_of_intr, 0),
1788017dc26SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
179999e12bbSAnthony Liguori };
180999e12bbSAnthony Liguori 
181999e12bbSAnthony Liguori static void xilinx_intc_class_init(ObjectClass *klass, void *data)
182999e12bbSAnthony Liguori {
18339bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
184999e12bbSAnthony Liguori 
18539bffca2SAnthony Liguori     dc->props = xilinx_intc_properties;
186ee6847d1SGerd Hoffmann }
187999e12bbSAnthony Liguori 
1888c43a6f0SAndreas Färber static const TypeInfo xilinx_intc_info = {
189cc3e064eSAndreas Färber     .name          = TYPE_XILINX_INTC,
19039bffca2SAnthony Liguori     .parent        = TYPE_SYS_BUS_DEVICE,
19139bffca2SAnthony Liguori     .instance_size = sizeof(struct xlx_pic),
192*a373cdb5SPeter Crosthwaite     .instance_init = xilinx_intc_init,
193999e12bbSAnthony Liguori     .class_init    = xilinx_intc_class_init,
194ee6847d1SGerd Hoffmann };
195ee6847d1SGerd Hoffmann 
19683f7d43aSAndreas Färber static void xilinx_intc_register_types(void)
19717628bc6SEdgar E. Iglesias {
19839bffca2SAnthony Liguori     type_register_static(&xilinx_intc_info);
19917628bc6SEdgar E. Iglesias }
20017628bc6SEdgar E. Iglesias 
20183f7d43aSAndreas Färber type_init(xilinx_intc_register_types)
202