xref: /qemu/hw/intc/heathrow_pic.c (revision ec7c2709d7f2ac04070ae7f10146f9ab733092c4)
1e68b9b2bSbellard /*
23cbee15bSj_mayer  * Heathrow PIC support (OldWorld PowerMac)
3e68b9b2bSbellard  *
43cbee15bSj_mayer  * Copyright (c) 2005-2007 Fabrice Bellard
53cbee15bSj_mayer  * Copyright (c) 2007 Jocelyn Mayer
6e68b9b2bSbellard  *
7e68b9b2bSbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
8e68b9b2bSbellard  * of this software and associated documentation files (the "Software"), to deal
9e68b9b2bSbellard  * in the Software without restriction, including without limitation the rights
10e68b9b2bSbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11e68b9b2bSbellard  * copies of the Software, and to permit persons to whom the Software is
12e68b9b2bSbellard  * furnished to do so, subject to the following conditions:
13e68b9b2bSbellard  *
14e68b9b2bSbellard  * The above copyright notice and this permission notice shall be included in
15e68b9b2bSbellard  * all copies or substantial portions of the Software.
16e68b9b2bSbellard  *
17e68b9b2bSbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18e68b9b2bSbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e68b9b2bSbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20e68b9b2bSbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21e68b9b2bSbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22e68b9b2bSbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23e68b9b2bSbellard  * THE SOFTWARE.
24e68b9b2bSbellard  */
2590191d07SPeter Maydell #include "qemu/osdep.h"
2683c9f4caSPaolo Bonzini #include "hw/hw.h"
2783c9f4caSPaolo Bonzini #include "hw/ppc/mac.h"
28086df4f3SMark Cave-Ayland #include "hw/intc/heathrow_pic.h"
29*ec7c2709SMark Cave-Ayland #include "trace.h"
30e68b9b2bSbellard 
31086df4f3SMark Cave-Ayland static inline int heathrow_check_irq(HeathrowPICState *pic)
32e68b9b2bSbellard {
33e68b9b2bSbellard     return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
34e68b9b2bSbellard }
35e68b9b2bSbellard 
36e68b9b2bSbellard /* update the CPU irq state */
37086df4f3SMark Cave-Ayland static void heathrow_update_irq(HeathrowState *s)
38e68b9b2bSbellard {
39086df4f3SMark Cave-Ayland     if (heathrow_check_irq(&s->pics[0]) ||
40086df4f3SMark Cave-Ayland             heathrow_check_irq(&s->pics[1])) {
413cbee15bSj_mayer         qemu_irq_raise(s->irqs[0]);
42e68b9b2bSbellard     } else {
433cbee15bSj_mayer         qemu_irq_lower(s->irqs[0]);
44e68b9b2bSbellard     }
45e68b9b2bSbellard }
46e68b9b2bSbellard 
47086df4f3SMark Cave-Ayland static void heathrow_write(void *opaque, hwaddr addr,
4823c5e4caSAvi Kivity                            uint64_t value, unsigned size)
49e68b9b2bSbellard {
50086df4f3SMark Cave-Ayland     HeathrowState *s = opaque;
51086df4f3SMark Cave-Ayland     HeathrowPICState *pic;
52e68b9b2bSbellard     unsigned int n;
53e68b9b2bSbellard 
54e68b9b2bSbellard     n = ((addr & 0xfff) - 0x10) >> 4;
55*ec7c2709SMark Cave-Ayland     trace_heathrow_write(addr, n, value);
56e68b9b2bSbellard     if (n >= 2)
57e68b9b2bSbellard         return;
58e68b9b2bSbellard     pic = &s->pics[n];
59e68b9b2bSbellard     switch(addr & 0xf) {
60e68b9b2bSbellard     case 0x04:
61e68b9b2bSbellard         pic->mask = value;
62086df4f3SMark Cave-Ayland         heathrow_update_irq(s);
63e68b9b2bSbellard         break;
64e68b9b2bSbellard     case 0x08:
65e68b9b2bSbellard         /* do not reset level triggered IRQs */
66e68b9b2bSbellard         value &= ~pic->level_triggered;
67e68b9b2bSbellard         pic->events &= ~value;
68086df4f3SMark Cave-Ayland         heathrow_update_irq(s);
69e68b9b2bSbellard         break;
70e68b9b2bSbellard     default:
71e68b9b2bSbellard         break;
72e68b9b2bSbellard     }
73e68b9b2bSbellard }
74e68b9b2bSbellard 
75086df4f3SMark Cave-Ayland static uint64_t heathrow_read(void *opaque, hwaddr addr,
7623c5e4caSAvi Kivity                               unsigned size)
77e68b9b2bSbellard {
78086df4f3SMark Cave-Ayland     HeathrowState *s = opaque;
79086df4f3SMark Cave-Ayland     HeathrowPICState *pic;
80e68b9b2bSbellard     unsigned int n;
81e68b9b2bSbellard     uint32_t value;
82e68b9b2bSbellard 
83e68b9b2bSbellard     n = ((addr & 0xfff) - 0x10) >> 4;
84e68b9b2bSbellard     if (n >= 2) {
85e68b9b2bSbellard         value = 0;
86e68b9b2bSbellard     } else {
87e68b9b2bSbellard         pic = &s->pics[n];
88e68b9b2bSbellard         switch(addr & 0xf) {
89e68b9b2bSbellard         case 0x0:
90e68b9b2bSbellard             value = pic->events;
91e68b9b2bSbellard             break;
92e68b9b2bSbellard         case 0x4:
93e68b9b2bSbellard             value = pic->mask;
94e68b9b2bSbellard             break;
95e68b9b2bSbellard         case 0xc:
96e68b9b2bSbellard             value = pic->levels;
97e68b9b2bSbellard             break;
98e68b9b2bSbellard         default:
99e68b9b2bSbellard             value = 0;
100e68b9b2bSbellard             break;
101e68b9b2bSbellard         }
102e68b9b2bSbellard     }
103*ec7c2709SMark Cave-Ayland     trace_heathrow_read(addr, n, value);
104e68b9b2bSbellard     return value;
105e68b9b2bSbellard }
106e68b9b2bSbellard 
107086df4f3SMark Cave-Ayland static const MemoryRegionOps heathrow_ops = {
108086df4f3SMark Cave-Ayland     .read = heathrow_read,
109086df4f3SMark Cave-Ayland     .write = heathrow_write,
1100157644cSAlexander Graf     .endianness = DEVICE_LITTLE_ENDIAN,
111e68b9b2bSbellard };
112e68b9b2bSbellard 
113086df4f3SMark Cave-Ayland static void heathrow_set_irq(void *opaque, int num, int level)
114e68b9b2bSbellard {
115086df4f3SMark Cave-Ayland     HeathrowState *s = opaque;
116086df4f3SMark Cave-Ayland     HeathrowPICState *pic;
117e68b9b2bSbellard     unsigned int irq_bit;
118*ec7c2709SMark Cave-Ayland     int last_level;
119e68b9b2bSbellard 
120e68b9b2bSbellard     pic = &s->pics[1 - (num >> 5)];
121e68b9b2bSbellard     irq_bit = 1 << (num & 0x1f);
122*ec7c2709SMark Cave-Ayland     last_level = (pic->levels & irq_bit) ? 1 : 0;
123*ec7c2709SMark Cave-Ayland 
124e68b9b2bSbellard     if (level) {
125e68b9b2bSbellard         pic->events |= irq_bit & ~pic->level_triggered;
126e68b9b2bSbellard         pic->levels |= irq_bit;
127e68b9b2bSbellard     } else {
128e68b9b2bSbellard         pic->levels &= ~irq_bit;
129e68b9b2bSbellard     }
130*ec7c2709SMark Cave-Ayland 
131*ec7c2709SMark Cave-Ayland     if (last_level != level) {
132*ec7c2709SMark Cave-Ayland         trace_heathrow_set_irq(num, level);
133*ec7c2709SMark Cave-Ayland     }
134*ec7c2709SMark Cave-Ayland 
135086df4f3SMark Cave-Ayland     heathrow_update_irq(s);
136e68b9b2bSbellard }
137e68b9b2bSbellard 
1384acd38ceSJuan Quintela static const VMStateDescription vmstate_heathrow_pic_one = {
1394acd38ceSJuan Quintela     .name = "heathrow_pic_one",
1404acd38ceSJuan Quintela     .version_id = 0,
1414acd38ceSJuan Quintela     .minimum_version_id = 0,
1424acd38ceSJuan Quintela     .fields = (VMStateField[]) {
143086df4f3SMark Cave-Ayland         VMSTATE_UINT32(events, HeathrowPICState),
144086df4f3SMark Cave-Ayland         VMSTATE_UINT32(mask, HeathrowPICState),
145086df4f3SMark Cave-Ayland         VMSTATE_UINT32(levels, HeathrowPICState),
146086df4f3SMark Cave-Ayland         VMSTATE_UINT32(level_triggered, HeathrowPICState),
1474acd38ceSJuan Quintela         VMSTATE_END_OF_LIST()
1489b64997fSblueswir1     }
1494acd38ceSJuan Quintela };
1509b64997fSblueswir1 
151086df4f3SMark Cave-Ayland static const VMStateDescription vmstate_heathrow = {
1524acd38ceSJuan Quintela     .name = "heathrow_pic",
1534acd38ceSJuan Quintela     .version_id = 1,
1544acd38ceSJuan Quintela     .minimum_version_id = 1,
1554acd38ceSJuan Quintela     .fields = (VMStateField[]) {
156086df4f3SMark Cave-Ayland         VMSTATE_STRUCT_ARRAY(pics, HeathrowState, 2, 1,
157086df4f3SMark Cave-Ayland                              vmstate_heathrow_pic_one, HeathrowPICState),
1584acd38ceSJuan Quintela         VMSTATE_END_OF_LIST()
1599b64997fSblueswir1     }
1604acd38ceSJuan Quintela };
1619b64997fSblueswir1 
162086df4f3SMark Cave-Ayland static void heathrow_reset(DeviceState *d)
1636e6b7363Sblueswir1 {
164086df4f3SMark Cave-Ayland     HeathrowState *s = HEATHROW(d);
1656e6b7363Sblueswir1 
1666e6b7363Sblueswir1     s->pics[0].level_triggered = 0;
1676e6b7363Sblueswir1     s->pics[1].level_triggered = 0x1ff00000;
1686e6b7363Sblueswir1 }
1696e6b7363Sblueswir1 
170086df4f3SMark Cave-Ayland static void heathrow_init(Object *obj)
171086df4f3SMark Cave-Ayland {
172086df4f3SMark Cave-Ayland     HeathrowState *s = HEATHROW(obj);
173086df4f3SMark Cave-Ayland 
174086df4f3SMark Cave-Ayland     memory_region_init_io(&s->mem, OBJECT(s), &heathrow_ops, s,
175086df4f3SMark Cave-Ayland                           "heathrow-pic", 0x1000);
176086df4f3SMark Cave-Ayland }
177086df4f3SMark Cave-Ayland 
17823c5e4caSAvi Kivity qemu_irq *heathrow_pic_init(MemoryRegion **pmem,
1793cbee15bSj_mayer                             int nb_cpus, qemu_irq **irqs)
180e68b9b2bSbellard {
181086df4f3SMark Cave-Ayland     DeviceState *d;
182086df4f3SMark Cave-Ayland     HeathrowState *s;
183e68b9b2bSbellard 
184086df4f3SMark Cave-Ayland     d = qdev_create(NULL, TYPE_HEATHROW);
185086df4f3SMark Cave-Ayland     qdev_init_nofail(d);
186086df4f3SMark Cave-Ayland 
187086df4f3SMark Cave-Ayland     s = HEATHROW(d);
1883cbee15bSj_mayer     /* only 1 CPU */
1893cbee15bSj_mayer     s->irqs = irqs[0];
190086df4f3SMark Cave-Ayland 
19123c5e4caSAvi Kivity     *pmem = &s->mem;
1923cbee15bSj_mayer 
193086df4f3SMark Cave-Ayland     return qemu_allocate_irqs(heathrow_set_irq, s, HEATHROW_NUM_IRQS);
194e68b9b2bSbellard }
195086df4f3SMark Cave-Ayland 
196086df4f3SMark Cave-Ayland static void heathrow_class_init(ObjectClass *oc, void *data)
197086df4f3SMark Cave-Ayland {
198086df4f3SMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(oc);
199086df4f3SMark Cave-Ayland 
200086df4f3SMark Cave-Ayland     dc->reset = heathrow_reset;
201086df4f3SMark Cave-Ayland     dc->vmsd = &vmstate_heathrow;
202086df4f3SMark Cave-Ayland     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
203086df4f3SMark Cave-Ayland }
204086df4f3SMark Cave-Ayland 
205086df4f3SMark Cave-Ayland static const TypeInfo heathrow_type_info = {
206086df4f3SMark Cave-Ayland     .name = TYPE_HEATHROW,
207086df4f3SMark Cave-Ayland     .parent = TYPE_SYS_BUS_DEVICE,
208086df4f3SMark Cave-Ayland     .instance_size = sizeof(HeathrowState),
209086df4f3SMark Cave-Ayland     .instance_init = heathrow_init,
210086df4f3SMark Cave-Ayland     .class_init = heathrow_class_init,
211086df4f3SMark Cave-Ayland };
212086df4f3SMark Cave-Ayland 
213086df4f3SMark Cave-Ayland static void heathrow_register_types(void)
214086df4f3SMark Cave-Ayland {
215086df4f3SMark Cave-Ayland     type_register_static(&heathrow_type_info);
216086df4f3SMark Cave-Ayland }
217086df4f3SMark Cave-Ayland 
218086df4f3SMark Cave-Ayland type_init(heathrow_register_types)
219