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
32 #define PIC_DPRINTF(fmt, args...) \
33 do { printf("PIC: " fmt , ##args); } while (0)
35 #define PIC_DPRINTF(fmt, args...)
38 typedef struct HeathrowPIC
{
42 uint32_t level_triggered
;
45 typedef struct HeathrowPICS
{
50 static inline int check_irq(HeathrowPIC
*pic
)
52 return (pic
->events
| (pic
->levels
& pic
->level_triggered
)) & pic
->mask
;
55 /* update the CPU irq state */
56 static void heathrow_pic_update(HeathrowPICS
*s
)
58 if (check_irq(&s
->pics
[0]) || check_irq(&s
->pics
[1])) {
59 qemu_irq_raise(s
->irqs
[0]);
61 qemu_irq_lower(s
->irqs
[0]);
65 static void pic_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
67 HeathrowPICS
*s
= opaque
;
71 #ifdef TARGET_WORDS_BIGENDIAN
72 value
= bswap32(value
);
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 uint32_t pic_readl (void *opaque
, target_phys_addr_t addr
)
97 HeathrowPICS
*s
= opaque
;
102 n
= ((addr
& 0xfff) - 0x10) >> 4;
122 PIC_DPRINTF("readl: " TARGET_FMT_plx
" %u: %08x\n", addr
, n
, value
);
123 #ifdef TARGET_WORDS_BIGENDIAN
124 value
= bswap32(value
);
129 static CPUWriteMemoryFunc
*pic_write
[] = {
135 static CPUReadMemoryFunc
*pic_read
[] = {
142 static void heathrow_pic_set_irq(void *opaque
, int num
, int level
)
144 HeathrowPICS
*s
= opaque
;
146 unsigned int irq_bit
;
150 static int last_level
[64];
151 if (last_level
[num
] != level
) {
152 PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num
, level
);
153 last_level
[num
] = level
;
157 pic
= &s
->pics
[1 - (num
>> 5)];
158 irq_bit
= 1 << (num
& 0x1f);
160 pic
->events
|= irq_bit
& ~pic
->level_triggered
;
161 pic
->levels
|= irq_bit
;
163 pic
->levels
&= ~irq_bit
;
165 heathrow_pic_update(s
);
168 static void heathrow_pic_save_one(QEMUFile
*f
, HeathrowPIC
*s
)
170 qemu_put_be32s(f
, &s
->events
);
171 qemu_put_be32s(f
, &s
->mask
);
172 qemu_put_be32s(f
, &s
->levels
);
173 qemu_put_be32s(f
, &s
->level_triggered
);
176 static void heathrow_pic_save(QEMUFile
*f
, void *opaque
)
178 HeathrowPICS
*s
= (HeathrowPICS
*)opaque
;
180 heathrow_pic_save_one(f
, &s
->pics
[0]);
181 heathrow_pic_save_one(f
, &s
->pics
[1]);
184 static void heathrow_pic_load_one(QEMUFile
*f
, HeathrowPIC
*s
)
186 qemu_get_be32s(f
, &s
->events
);
187 qemu_get_be32s(f
, &s
->mask
);
188 qemu_get_be32s(f
, &s
->levels
);
189 qemu_get_be32s(f
, &s
->level_triggered
);
192 static int heathrow_pic_load(QEMUFile
*f
, void *opaque
, int version_id
)
194 HeathrowPICS
*s
= (HeathrowPICS
*)opaque
;
199 heathrow_pic_load_one(f
, &s
->pics
[0]);
200 heathrow_pic_load_one(f
, &s
->pics
[1]);
205 static void heathrow_pic_reset_one(HeathrowPIC
*s
)
207 memset(s
, '\0', sizeof(HeathrowPIC
));
210 static void heathrow_pic_reset(void *opaque
)
212 HeathrowPICS
*s
= opaque
;
214 heathrow_pic_reset_one(&s
->pics
[0]);
215 heathrow_pic_reset_one(&s
->pics
[1]);
217 s
->pics
[0].level_triggered
= 0;
218 s
->pics
[1].level_triggered
= 0x1ff00000;
221 qemu_irq
*heathrow_pic_init(int *pmem_index
,
222 int nb_cpus
, qemu_irq
**irqs
)
226 s
= qemu_mallocz(sizeof(HeathrowPICS
));
229 *pmem_index
= cpu_register_io_memory(0, pic_read
, pic_write
, s
);
231 register_savevm("heathrow_pic", -1, 1, heathrow_pic_save
,
232 heathrow_pic_load
, s
);
233 qemu_register_reset(heathrow_pic_reset
, s
);
234 heathrow_pic_reset(s
);
235 return qemu_allocate_irqs(heathrow_pic_set_irq
, s
, 64);