]> git.proxmox.com Git - qemu.git/blame - hw/heathrow_pic.c
slirp: Replace m_freem with m_free
[qemu.git] / hw / heathrow_pic.c
CommitLineData
e68b9b2b 1/*
3cbee15b 2 * Heathrow PIC support (OldWorld PowerMac)
5fafdf24 3 *
3cbee15b
JM
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
5fafdf24 6 *
e68b9b2b
FB
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
87ecb68b 25#include "hw.h"
3cbee15b 26#include "ppc_mac.h"
e68b9b2b 27
ea026b2f
BS
28/* debug PIC */
29//#define DEBUG_PIC
30
31#ifdef DEBUG_PIC
001faf32
BS
32#define PIC_DPRINTF(fmt, ...) \
33 do { printf("PIC: " fmt , ## __VA_ARGS__); } while (0)
ea026b2f 34#else
001faf32 35#define PIC_DPRINTF(fmt, ...)
ea026b2f 36#endif
e68b9b2b
FB
37
38typedef struct HeathrowPIC {
39 uint32_t events;
40 uint32_t mask;
41 uint32_t levels;
42 uint32_t level_triggered;
43} HeathrowPIC;
44
d537cf6c 45typedef struct HeathrowPICS {
e68b9b2b 46 HeathrowPIC pics[2];
3cbee15b 47 qemu_irq *irqs;
d537cf6c 48} HeathrowPICS;
e68b9b2b
FB
49
50static inline int check_irq(HeathrowPIC *pic)
51{
52 return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
53}
54
55/* update the CPU irq state */
56static void heathrow_pic_update(HeathrowPICS *s)
57{
58 if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
3cbee15b 59 qemu_irq_raise(s->irqs[0]);
e68b9b2b 60 } else {
3cbee15b 61 qemu_irq_lower(s->irqs[0]);
e68b9b2b
FB
62 }
63}
64
c227f099 65static void pic_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
e68b9b2b
FB
66{
67 HeathrowPICS *s = opaque;
68 HeathrowPIC *pic;
69 unsigned int n;
70
e68b9b2b 71 n = ((addr & 0xfff) - 0x10) >> 4;
ea026b2f 72 PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
e68b9b2b
FB
73 if (n >= 2)
74 return;
75 pic = &s->pics[n];
76 switch(addr & 0xf) {
77 case 0x04:
78 pic->mask = value;
79 heathrow_pic_update(s);
80 break;
81 case 0x08:
82 /* do not reset level triggered IRQs */
83 value &= ~pic->level_triggered;
84 pic->events &= ~value;
85 heathrow_pic_update(s);
86 break;
87 default:
88 break;
89 }
90}
91
c227f099 92static uint32_t pic_readl (void *opaque, target_phys_addr_t addr)
e68b9b2b
FB
93{
94 HeathrowPICS *s = opaque;
95 HeathrowPIC *pic;
96 unsigned int n;
97 uint32_t value;
3b46e624 98
e68b9b2b
FB
99 n = ((addr & 0xfff) - 0x10) >> 4;
100 if (n >= 2) {
101 value = 0;
102 } else {
103 pic = &s->pics[n];
104 switch(addr & 0xf) {
105 case 0x0:
106 value = pic->events;
107 break;
108 case 0x4:
109 value = pic->mask;
110 break;
111 case 0xc:
112 value = pic->levels;
113 break;
114 default:
115 value = 0;
116 break;
117 }
118 }
ea026b2f 119 PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
e68b9b2b
FB
120 return value;
121}
122
d60efc6b 123static CPUWriteMemoryFunc * const pic_write[] = {
e68b9b2b
FB
124 &pic_writel,
125 &pic_writel,
126 &pic_writel,
127};
128
d60efc6b 129static CPUReadMemoryFunc * const pic_read[] = {
e68b9b2b
FB
130 &pic_readl,
131 &pic_readl,
132 &pic_readl,
133};
134
135
d537cf6c 136static void heathrow_pic_set_irq(void *opaque, int num, int level)
e68b9b2b
FB
137{
138 HeathrowPICS *s = opaque;
139 HeathrowPIC *pic;
140 unsigned int irq_bit;
141
142#if defined(DEBUG)
143 {
144 static int last_level[64];
145 if (last_level[num] != level) {
ea026b2f 146 PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
e68b9b2b
FB
147 last_level[num] = level;
148 }
149 }
150#endif
151 pic = &s->pics[1 - (num >> 5)];
152 irq_bit = 1 << (num & 0x1f);
153 if (level) {
154 pic->events |= irq_bit & ~pic->level_triggered;
155 pic->levels |= irq_bit;
156 } else {
157 pic->levels &= ~irq_bit;
158 }
159 heathrow_pic_update(s);
160}
161
4acd38ce
JQ
162static const VMStateDescription vmstate_heathrow_pic_one = {
163 .name = "heathrow_pic_one",
164 .version_id = 0,
165 .minimum_version_id = 0,
166 .minimum_version_id_old = 0,
167 .fields = (VMStateField[]) {
168 VMSTATE_UINT32(events, HeathrowPIC),
169 VMSTATE_UINT32(mask, HeathrowPIC),
170 VMSTATE_UINT32(levels, HeathrowPIC),
171 VMSTATE_UINT32(level_triggered, HeathrowPIC),
172 VMSTATE_END_OF_LIST()
173 }
174};
9b64997f 175
4acd38ce
JQ
176static const VMStateDescription vmstate_heathrow_pic = {
177 .name = "heathrow_pic",
178 .version_id = 1,
179 .minimum_version_id = 1,
180 .minimum_version_id_old = 1,
181 .fields = (VMStateField[]) {
182 VMSTATE_STRUCT_ARRAY(pics, HeathrowPICS, 2, 1,
183 vmstate_heathrow_pic_one, HeathrowPIC),
184 VMSTATE_END_OF_LIST()
185 }
186};
9b64997f 187
6e6b7363
BS
188static void heathrow_pic_reset_one(HeathrowPIC *s)
189{
190 memset(s, '\0', sizeof(HeathrowPIC));
191}
192
193static void heathrow_pic_reset(void *opaque)
194{
195 HeathrowPICS *s = opaque;
196
197 heathrow_pic_reset_one(&s->pics[0]);
198 heathrow_pic_reset_one(&s->pics[1]);
199
200 s->pics[0].level_triggered = 0;
201 s->pics[1].level_triggered = 0x1ff00000;
202}
203
3cbee15b
JM
204qemu_irq *heathrow_pic_init(int *pmem_index,
205 int nb_cpus, qemu_irq **irqs)
e68b9b2b
FB
206{
207 HeathrowPICS *s;
3b46e624 208
e68b9b2b 209 s = qemu_mallocz(sizeof(HeathrowPICS));
3cbee15b
JM
210 /* only 1 CPU */
211 s->irqs = irqs[0];
2507c12a 212 *pmem_index = cpu_register_io_memory(pic_read, pic_write, s,
b093c1a3 213 DEVICE_LITTLE_ENDIAN);
3cbee15b 214
4acd38ce 215 vmstate_register(NULL, -1, &vmstate_heathrow_pic, s);
a08d4367 216 qemu_register_reset(heathrow_pic_reset, s);
d537cf6c 217 return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);
e68b9b2b 218}