Statistics
| Branch: | Revision:

root / hw / heathrow_pic.c @ e7b43f7e

History | View | Annotate | Download (5.9 kB)

1 e68b9b2b bellard
/*
2 3cbee15b j_mayer
 * Heathrow PIC support (OldWorld PowerMac)
3 5fafdf24 ths
 *
4 3cbee15b j_mayer
 * Copyright (c) 2005-2007 Fabrice Bellard
5 3cbee15b j_mayer
 * Copyright (c) 2007 Jocelyn Mayer
6 5fafdf24 ths
 *
7 e68b9b2b bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 e68b9b2b bellard
 * of this software and associated documentation files (the "Software"), to deal
9 e68b9b2b bellard
 * in the Software without restriction, including without limitation the rights
10 e68b9b2b bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 e68b9b2b bellard
 * copies of the Software, and to permit persons to whom the Software is
12 e68b9b2b bellard
 * furnished to do so, subject to the following conditions:
13 e68b9b2b bellard
 *
14 e68b9b2b bellard
 * The above copyright notice and this permission notice shall be included in
15 e68b9b2b bellard
 * all copies or substantial portions of the Software.
16 e68b9b2b bellard
 *
17 e68b9b2b bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 e68b9b2b bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 e68b9b2b bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 e68b9b2b bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 e68b9b2b bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 e68b9b2b bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 e68b9b2b bellard
 * THE SOFTWARE.
24 e68b9b2b bellard
 */
25 87ecb68b pbrook
#include "hw.h"
26 3cbee15b j_mayer
#include "ppc_mac.h"
27 e68b9b2b bellard
28 ea026b2f blueswir1
/* debug PIC */
29 ea026b2f blueswir1
//#define DEBUG_PIC
30 ea026b2f blueswir1
31 ea026b2f blueswir1
#ifdef DEBUG_PIC
32 001faf32 Blue Swirl
#define PIC_DPRINTF(fmt, ...)                                   \
33 001faf32 Blue Swirl
    do { printf("PIC: " fmt , ## __VA_ARGS__); } while (0)
34 ea026b2f blueswir1
#else
35 001faf32 Blue Swirl
#define PIC_DPRINTF(fmt, ...)
36 ea026b2f blueswir1
#endif
37 e68b9b2b bellard
38 e68b9b2b bellard
typedef struct HeathrowPIC {
39 e68b9b2b bellard
    uint32_t events;
40 e68b9b2b bellard
    uint32_t mask;
41 e68b9b2b bellard
    uint32_t levels;
42 e68b9b2b bellard
    uint32_t level_triggered;
43 e68b9b2b bellard
} HeathrowPIC;
44 e68b9b2b bellard
45 d537cf6c pbrook
typedef struct HeathrowPICS {
46 e68b9b2b bellard
    HeathrowPIC pics[2];
47 3cbee15b j_mayer
    qemu_irq *irqs;
48 d537cf6c pbrook
} HeathrowPICS;
49 e68b9b2b bellard
50 e68b9b2b bellard
static inline int check_irq(HeathrowPIC *pic)
51 e68b9b2b bellard
{
52 e68b9b2b bellard
    return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
53 e68b9b2b bellard
}
54 e68b9b2b bellard
55 e68b9b2b bellard
/* update the CPU irq state */
56 e68b9b2b bellard
static void heathrow_pic_update(HeathrowPICS *s)
57 e68b9b2b bellard
{
58 e68b9b2b bellard
    if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
59 3cbee15b j_mayer
        qemu_irq_raise(s->irqs[0]);
60 e68b9b2b bellard
    } else {
61 3cbee15b j_mayer
        qemu_irq_lower(s->irqs[0]);
62 e68b9b2b bellard
    }
63 e68b9b2b bellard
}
64 e68b9b2b bellard
65 c227f099 Anthony Liguori
static void pic_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
66 e68b9b2b bellard
{
67 e68b9b2b bellard
    HeathrowPICS *s = opaque;
68 e68b9b2b bellard
    HeathrowPIC *pic;
69 e68b9b2b bellard
    unsigned int n;
70 e68b9b2b bellard
71 e68b9b2b bellard
    value = bswap32(value);
72 e68b9b2b bellard
    n = ((addr & 0xfff) - 0x10) >> 4;
73 ea026b2f blueswir1
    PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
74 e68b9b2b bellard
    if (n >= 2)
75 e68b9b2b bellard
        return;
76 e68b9b2b bellard
    pic = &s->pics[n];
77 e68b9b2b bellard
    switch(addr & 0xf) {
78 e68b9b2b bellard
    case 0x04:
79 e68b9b2b bellard
        pic->mask = value;
80 e68b9b2b bellard
        heathrow_pic_update(s);
81 e68b9b2b bellard
        break;
82 e68b9b2b bellard
    case 0x08:
83 e68b9b2b bellard
        /* do not reset level triggered IRQs */
84 e68b9b2b bellard
        value &= ~pic->level_triggered;
85 e68b9b2b bellard
        pic->events &= ~value;
86 e68b9b2b bellard
        heathrow_pic_update(s);
87 e68b9b2b bellard
        break;
88 e68b9b2b bellard
    default:
89 e68b9b2b bellard
        break;
90 e68b9b2b bellard
    }
91 e68b9b2b bellard
}
92 e68b9b2b bellard
93 c227f099 Anthony Liguori
static uint32_t pic_readl (void *opaque, target_phys_addr_t addr)
94 e68b9b2b bellard
{
95 e68b9b2b bellard
    HeathrowPICS *s = opaque;
96 e68b9b2b bellard
    HeathrowPIC *pic;
97 e68b9b2b bellard
    unsigned int n;
98 e68b9b2b bellard
    uint32_t value;
99 3b46e624 ths
100 e68b9b2b bellard
    n = ((addr & 0xfff) - 0x10) >> 4;
101 e68b9b2b bellard
    if (n >= 2) {
102 e68b9b2b bellard
        value = 0;
103 e68b9b2b bellard
    } else {
104 e68b9b2b bellard
        pic = &s->pics[n];
105 e68b9b2b bellard
        switch(addr & 0xf) {
106 e68b9b2b bellard
        case 0x0:
107 e68b9b2b bellard
            value = pic->events;
108 e68b9b2b bellard
            break;
109 e68b9b2b bellard
        case 0x4:
110 e68b9b2b bellard
            value = pic->mask;
111 e68b9b2b bellard
            break;
112 e68b9b2b bellard
        case 0xc:
113 e68b9b2b bellard
            value = pic->levels;
114 e68b9b2b bellard
            break;
115 e68b9b2b bellard
        default:
116 e68b9b2b bellard
            value = 0;
117 e68b9b2b bellard
            break;
118 e68b9b2b bellard
        }
119 e68b9b2b bellard
    }
120 ea026b2f blueswir1
    PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
121 e68b9b2b bellard
    value = bswap32(value);
122 e68b9b2b bellard
    return value;
123 e68b9b2b bellard
}
124 e68b9b2b bellard
125 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const pic_write[] = {
126 e68b9b2b bellard
    &pic_writel,
127 e68b9b2b bellard
    &pic_writel,
128 e68b9b2b bellard
    &pic_writel,
129 e68b9b2b bellard
};
130 e68b9b2b bellard
131 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const pic_read[] = {
132 e68b9b2b bellard
    &pic_readl,
133 e68b9b2b bellard
    &pic_readl,
134 e68b9b2b bellard
    &pic_readl,
135 e68b9b2b bellard
};
136 e68b9b2b bellard
137 e68b9b2b bellard
138 d537cf6c pbrook
static void heathrow_pic_set_irq(void *opaque, int num, int level)
139 e68b9b2b bellard
{
140 e68b9b2b bellard
    HeathrowPICS *s = opaque;
141 e68b9b2b bellard
    HeathrowPIC *pic;
142 e68b9b2b bellard
    unsigned int irq_bit;
143 e68b9b2b bellard
144 e68b9b2b bellard
#if defined(DEBUG)
145 e68b9b2b bellard
    {
146 e68b9b2b bellard
        static int last_level[64];
147 e68b9b2b bellard
        if (last_level[num] != level) {
148 ea026b2f blueswir1
            PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
149 e68b9b2b bellard
            last_level[num] = level;
150 e68b9b2b bellard
        }
151 e68b9b2b bellard
    }
152 e68b9b2b bellard
#endif
153 e68b9b2b bellard
    pic = &s->pics[1 - (num >> 5)];
154 e68b9b2b bellard
    irq_bit = 1 << (num & 0x1f);
155 e68b9b2b bellard
    if (level) {
156 e68b9b2b bellard
        pic->events |= irq_bit & ~pic->level_triggered;
157 e68b9b2b bellard
        pic->levels |= irq_bit;
158 e68b9b2b bellard
    } else {
159 e68b9b2b bellard
        pic->levels &= ~irq_bit;
160 e68b9b2b bellard
    }
161 e68b9b2b bellard
    heathrow_pic_update(s);
162 e68b9b2b bellard
}
163 e68b9b2b bellard
164 9b64997f blueswir1
static void heathrow_pic_save_one(QEMUFile *f, HeathrowPIC *s)
165 9b64997f blueswir1
{
166 9b64997f blueswir1
    qemu_put_be32s(f, &s->events);
167 9b64997f blueswir1
    qemu_put_be32s(f, &s->mask);
168 9b64997f blueswir1
    qemu_put_be32s(f, &s->levels);
169 9b64997f blueswir1
    qemu_put_be32s(f, &s->level_triggered);
170 9b64997f blueswir1
}
171 9b64997f blueswir1
172 9b64997f blueswir1
static void heathrow_pic_save(QEMUFile *f, void *opaque)
173 9b64997f blueswir1
{
174 9b64997f blueswir1
    HeathrowPICS *s = (HeathrowPICS *)opaque;
175 9b64997f blueswir1
176 9b64997f blueswir1
    heathrow_pic_save_one(f, &s->pics[0]);
177 9b64997f blueswir1
    heathrow_pic_save_one(f, &s->pics[1]);
178 9b64997f blueswir1
}
179 9b64997f blueswir1
180 9b64997f blueswir1
static void heathrow_pic_load_one(QEMUFile *f, HeathrowPIC *s)
181 9b64997f blueswir1
{
182 9b64997f blueswir1
    qemu_get_be32s(f, &s->events);
183 9b64997f blueswir1
    qemu_get_be32s(f, &s->mask);
184 9b64997f blueswir1
    qemu_get_be32s(f, &s->levels);
185 9b64997f blueswir1
    qemu_get_be32s(f, &s->level_triggered);
186 9b64997f blueswir1
}
187 9b64997f blueswir1
188 9b64997f blueswir1
static int heathrow_pic_load(QEMUFile *f, void *opaque, int version_id)
189 9b64997f blueswir1
{
190 9b64997f blueswir1
    HeathrowPICS *s = (HeathrowPICS *)opaque;
191 9b64997f blueswir1
192 9b64997f blueswir1
    if (version_id != 1)
193 9b64997f blueswir1
        return -EINVAL;
194 9b64997f blueswir1
195 9b64997f blueswir1
    heathrow_pic_load_one(f, &s->pics[0]);
196 9b64997f blueswir1
    heathrow_pic_load_one(f, &s->pics[1]);
197 9b64997f blueswir1
198 9b64997f blueswir1
    return 0;
199 9b64997f blueswir1
}
200 9b64997f blueswir1
201 6e6b7363 blueswir1
static void heathrow_pic_reset_one(HeathrowPIC *s)
202 6e6b7363 blueswir1
{
203 6e6b7363 blueswir1
    memset(s, '\0', sizeof(HeathrowPIC));
204 6e6b7363 blueswir1
}
205 6e6b7363 blueswir1
206 6e6b7363 blueswir1
static void heathrow_pic_reset(void *opaque)
207 6e6b7363 blueswir1
{
208 6e6b7363 blueswir1
    HeathrowPICS *s = opaque;
209 6e6b7363 blueswir1
210 6e6b7363 blueswir1
    heathrow_pic_reset_one(&s->pics[0]);
211 6e6b7363 blueswir1
    heathrow_pic_reset_one(&s->pics[1]);
212 6e6b7363 blueswir1
213 6e6b7363 blueswir1
    s->pics[0].level_triggered = 0;
214 6e6b7363 blueswir1
    s->pics[1].level_triggered = 0x1ff00000;
215 6e6b7363 blueswir1
}
216 6e6b7363 blueswir1
217 3cbee15b j_mayer
qemu_irq *heathrow_pic_init(int *pmem_index,
218 3cbee15b j_mayer
                            int nb_cpus, qemu_irq **irqs)
219 e68b9b2b bellard
{
220 e68b9b2b bellard
    HeathrowPICS *s;
221 3b46e624 ths
222 e68b9b2b bellard
    s = qemu_mallocz(sizeof(HeathrowPICS));
223 3cbee15b j_mayer
    /* only 1 CPU */
224 3cbee15b j_mayer
    s->irqs = irqs[0];
225 1eed09cb Avi Kivity
    *pmem_index = cpu_register_io_memory(pic_read, pic_write, s);
226 3cbee15b j_mayer
227 0be71e32 Alex Williamson
    register_savevm(NULL, "heathrow_pic", -1, 1, heathrow_pic_save,
228 9b64997f blueswir1
                    heathrow_pic_load, s);
229 a08d4367 Jan Kiszka
    qemu_register_reset(heathrow_pic_reset, s);
230 d537cf6c pbrook
    return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);
231 e68b9b2b bellard
}