2 * Heathrow PIC support (OldWorld PowerMac)
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "hw/ppc/mac.h"
33 #define PIC_DPRINTF(fmt, ...) \
34 do { printf("PIC: " fmt , ## __VA_ARGS__); } while (0)
36 #define PIC_DPRINTF(fmt, ...)
39 typedef struct HeathrowPIC {
43 uint32_t level_triggered;
46 typedef struct HeathrowPICS {
52 static inline int check_irq(HeathrowPIC *pic)
54 return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
57 /* update the CPU irq state */
58 static void heathrow_pic_update(HeathrowPICS *s)
60 if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
61 qemu_irq_raise(s->irqs[0]);
63 qemu_irq_lower(s->irqs[0]);
67 static void pic_write(void *opaque, hwaddr addr,
68 uint64_t value, unsigned size)
70 HeathrowPICS *s = opaque;
74 n = ((addr & 0xfff) - 0x10) >> 4;
75 PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
82 heathrow_pic_update(s);
85 /* do not reset level triggered IRQs */
86 value &= ~pic->level_triggered;
87 pic->events &= ~value;
88 heathrow_pic_update(s);
95 static uint64_t pic_read(void *opaque, hwaddr addr,
98 HeathrowPICS *s = opaque;
103 n = ((addr & 0xfff) - 0x10) >> 4;
123 PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
127 static const MemoryRegionOps heathrow_pic_ops = {
130 .endianness = DEVICE_LITTLE_ENDIAN,
133 static void heathrow_pic_set_irq(void *opaque, int num, int level)
135 HeathrowPICS *s = opaque;
137 unsigned int irq_bit;
141 static int last_level[64];
142 if (last_level[num] != level) {
143 PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
144 last_level[num] = level;
148 pic = &s->pics[1 - (num >> 5)];
149 irq_bit = 1 << (num & 0x1f);
151 pic->events |= irq_bit & ~pic->level_triggered;
152 pic->levels |= irq_bit;
154 pic->levels &= ~irq_bit;
156 heathrow_pic_update(s);
159 static const VMStateDescription vmstate_heathrow_pic_one = {
160 .name = "heathrow_pic_one",
162 .minimum_version_id = 0,
163 .fields = (VMStateField[]) {
164 VMSTATE_UINT32(events, HeathrowPIC),
165 VMSTATE_UINT32(mask, HeathrowPIC),
166 VMSTATE_UINT32(levels, HeathrowPIC),
167 VMSTATE_UINT32(level_triggered, HeathrowPIC),
168 VMSTATE_END_OF_LIST()
172 static const VMStateDescription vmstate_heathrow_pic = {
173 .name = "heathrow_pic",
175 .minimum_version_id = 1,
176 .fields = (VMStateField[]) {
177 VMSTATE_STRUCT_ARRAY(pics, HeathrowPICS, 2, 1,
178 vmstate_heathrow_pic_one, HeathrowPIC),
179 VMSTATE_END_OF_LIST()
183 static void heathrow_pic_reset_one(HeathrowPIC *s)
185 memset(s, '\0', sizeof(HeathrowPIC));
188 static void heathrow_pic_reset(void *opaque)
190 HeathrowPICS *s = opaque;
192 heathrow_pic_reset_one(&s->pics[0]);
193 heathrow_pic_reset_one(&s->pics[1]);
195 s->pics[0].level_triggered = 0;
196 s->pics[1].level_triggered = 0x1ff00000;
199 qemu_irq *heathrow_pic_init(MemoryRegion **pmem,
200 int nb_cpus, qemu_irq **irqs)
204 s = g_malloc0(sizeof(HeathrowPICS));
207 memory_region_init_io(&s->mem, NULL, &heathrow_pic_ops, s,
208 "heathrow-pic", 0x1000);
211 vmstate_register(NULL, -1, &vmstate_heathrow_pic, s);
212 qemu_register_reset(heathrow_pic_reset, s);
213 return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);