Statistics
| Branch: | Revision:

root / hw / intc / heathrow_pic.c @ a8aec295

History | View | Annotate | Download (5.8 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 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
26 83c9f4ca Paolo Bonzini
#include "hw/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 23c5e4ca Avi Kivity
    MemoryRegion mem;
47 e68b9b2b bellard
    HeathrowPIC pics[2];
48 3cbee15b j_mayer
    qemu_irq *irqs;
49 d537cf6c pbrook
} HeathrowPICS;
50 e68b9b2b bellard
51 e68b9b2b bellard
static inline int check_irq(HeathrowPIC *pic)
52 e68b9b2b bellard
{
53 e68b9b2b bellard
    return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
54 e68b9b2b bellard
}
55 e68b9b2b bellard
56 e68b9b2b bellard
/* update the CPU irq state */
57 e68b9b2b bellard
static void heathrow_pic_update(HeathrowPICS *s)
58 e68b9b2b bellard
{
59 e68b9b2b bellard
    if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
60 3cbee15b j_mayer
        qemu_irq_raise(s->irqs[0]);
61 e68b9b2b bellard
    } else {
62 3cbee15b j_mayer
        qemu_irq_lower(s->irqs[0]);
63 e68b9b2b bellard
    }
64 e68b9b2b bellard
}
65 e68b9b2b bellard
66 a8170e5e Avi Kivity
static void pic_write(void *opaque, hwaddr addr,
67 23c5e4ca Avi Kivity
                      uint64_t value, unsigned size)
68 e68b9b2b bellard
{
69 e68b9b2b bellard
    HeathrowPICS *s = opaque;
70 e68b9b2b bellard
    HeathrowPIC *pic;
71 e68b9b2b bellard
    unsigned int n;
72 e68b9b2b bellard
73 e68b9b2b bellard
    n = ((addr & 0xfff) - 0x10) >> 4;
74 ea026b2f blueswir1
    PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
75 e68b9b2b bellard
    if (n >= 2)
76 e68b9b2b bellard
        return;
77 e68b9b2b bellard
    pic = &s->pics[n];
78 e68b9b2b bellard
    switch(addr & 0xf) {
79 e68b9b2b bellard
    case 0x04:
80 e68b9b2b bellard
        pic->mask = value;
81 e68b9b2b bellard
        heathrow_pic_update(s);
82 e68b9b2b bellard
        break;
83 e68b9b2b bellard
    case 0x08:
84 e68b9b2b bellard
        /* do not reset level triggered IRQs */
85 e68b9b2b bellard
        value &= ~pic->level_triggered;
86 e68b9b2b bellard
        pic->events &= ~value;
87 e68b9b2b bellard
        heathrow_pic_update(s);
88 e68b9b2b bellard
        break;
89 e68b9b2b bellard
    default:
90 e68b9b2b bellard
        break;
91 e68b9b2b bellard
    }
92 e68b9b2b bellard
}
93 e68b9b2b bellard
94 a8170e5e Avi Kivity
static uint64_t pic_read(void *opaque, hwaddr addr,
95 23c5e4ca Avi Kivity
                         unsigned size)
96 e68b9b2b bellard
{
97 e68b9b2b bellard
    HeathrowPICS *s = opaque;
98 e68b9b2b bellard
    HeathrowPIC *pic;
99 e68b9b2b bellard
    unsigned int n;
100 e68b9b2b bellard
    uint32_t value;
101 3b46e624 ths
102 e68b9b2b bellard
    n = ((addr & 0xfff) - 0x10) >> 4;
103 e68b9b2b bellard
    if (n >= 2) {
104 e68b9b2b bellard
        value = 0;
105 e68b9b2b bellard
    } else {
106 e68b9b2b bellard
        pic = &s->pics[n];
107 e68b9b2b bellard
        switch(addr & 0xf) {
108 e68b9b2b bellard
        case 0x0:
109 e68b9b2b bellard
            value = pic->events;
110 e68b9b2b bellard
            break;
111 e68b9b2b bellard
        case 0x4:
112 e68b9b2b bellard
            value = pic->mask;
113 e68b9b2b bellard
            break;
114 e68b9b2b bellard
        case 0xc:
115 e68b9b2b bellard
            value = pic->levels;
116 e68b9b2b bellard
            break;
117 e68b9b2b bellard
        default:
118 e68b9b2b bellard
            value = 0;
119 e68b9b2b bellard
            break;
120 e68b9b2b bellard
        }
121 e68b9b2b bellard
    }
122 ea026b2f blueswir1
    PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
123 e68b9b2b bellard
    return value;
124 e68b9b2b bellard
}
125 e68b9b2b bellard
126 23c5e4ca Avi Kivity
static const MemoryRegionOps heathrow_pic_ops = {
127 23c5e4ca Avi Kivity
    .read = pic_read,
128 23c5e4ca Avi Kivity
    .write = pic_write,
129 0157644c Alexander Graf
    .endianness = DEVICE_LITTLE_ENDIAN,
130 e68b9b2b bellard
};
131 e68b9b2b bellard
132 d537cf6c pbrook
static void heathrow_pic_set_irq(void *opaque, int num, int level)
133 e68b9b2b bellard
{
134 e68b9b2b bellard
    HeathrowPICS *s = opaque;
135 e68b9b2b bellard
    HeathrowPIC *pic;
136 e68b9b2b bellard
    unsigned int irq_bit;
137 e68b9b2b bellard
138 e68b9b2b bellard
#if defined(DEBUG)
139 e68b9b2b bellard
    {
140 e68b9b2b bellard
        static int last_level[64];
141 e68b9b2b bellard
        if (last_level[num] != level) {
142 ea026b2f blueswir1
            PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
143 e68b9b2b bellard
            last_level[num] = level;
144 e68b9b2b bellard
        }
145 e68b9b2b bellard
    }
146 e68b9b2b bellard
#endif
147 e68b9b2b bellard
    pic = &s->pics[1 - (num >> 5)];
148 e68b9b2b bellard
    irq_bit = 1 << (num & 0x1f);
149 e68b9b2b bellard
    if (level) {
150 e68b9b2b bellard
        pic->events |= irq_bit & ~pic->level_triggered;
151 e68b9b2b bellard
        pic->levels |= irq_bit;
152 e68b9b2b bellard
    } else {
153 e68b9b2b bellard
        pic->levels &= ~irq_bit;
154 e68b9b2b bellard
    }
155 e68b9b2b bellard
    heathrow_pic_update(s);
156 e68b9b2b bellard
}
157 e68b9b2b bellard
158 4acd38ce Juan Quintela
static const VMStateDescription vmstate_heathrow_pic_one = {
159 4acd38ce Juan Quintela
    .name = "heathrow_pic_one",
160 4acd38ce Juan Quintela
    .version_id = 0,
161 4acd38ce Juan Quintela
    .minimum_version_id = 0,
162 4acd38ce Juan Quintela
    .minimum_version_id_old = 0,
163 4acd38ce Juan Quintela
    .fields      = (VMStateField[]) {
164 4acd38ce Juan Quintela
        VMSTATE_UINT32(events, HeathrowPIC),
165 4acd38ce Juan Quintela
        VMSTATE_UINT32(mask, HeathrowPIC),
166 4acd38ce Juan Quintela
        VMSTATE_UINT32(levels, HeathrowPIC),
167 4acd38ce Juan Quintela
        VMSTATE_UINT32(level_triggered, HeathrowPIC),
168 4acd38ce Juan Quintela
        VMSTATE_END_OF_LIST()
169 4acd38ce Juan Quintela
    }
170 4acd38ce Juan Quintela
};
171 9b64997f blueswir1
172 4acd38ce Juan Quintela
static const VMStateDescription vmstate_heathrow_pic = {
173 4acd38ce Juan Quintela
    .name = "heathrow_pic",
174 4acd38ce Juan Quintela
    .version_id = 1,
175 4acd38ce Juan Quintela
    .minimum_version_id = 1,
176 4acd38ce Juan Quintela
    .minimum_version_id_old = 1,
177 4acd38ce Juan Quintela
    .fields      = (VMStateField[]) {
178 4acd38ce Juan Quintela
        VMSTATE_STRUCT_ARRAY(pics, HeathrowPICS, 2, 1,
179 4acd38ce Juan Quintela
                             vmstate_heathrow_pic_one, HeathrowPIC),
180 4acd38ce Juan Quintela
        VMSTATE_END_OF_LIST()
181 4acd38ce Juan Quintela
    }
182 4acd38ce Juan Quintela
};
183 9b64997f blueswir1
184 6e6b7363 blueswir1
static void heathrow_pic_reset_one(HeathrowPIC *s)
185 6e6b7363 blueswir1
{
186 6e6b7363 blueswir1
    memset(s, '\0', sizeof(HeathrowPIC));
187 6e6b7363 blueswir1
}
188 6e6b7363 blueswir1
189 6e6b7363 blueswir1
static void heathrow_pic_reset(void *opaque)
190 6e6b7363 blueswir1
{
191 6e6b7363 blueswir1
    HeathrowPICS *s = opaque;
192 6e6b7363 blueswir1
193 6e6b7363 blueswir1
    heathrow_pic_reset_one(&s->pics[0]);
194 6e6b7363 blueswir1
    heathrow_pic_reset_one(&s->pics[1]);
195 6e6b7363 blueswir1
196 6e6b7363 blueswir1
    s->pics[0].level_triggered = 0;
197 6e6b7363 blueswir1
    s->pics[1].level_triggered = 0x1ff00000;
198 6e6b7363 blueswir1
}
199 6e6b7363 blueswir1
200 23c5e4ca Avi Kivity
qemu_irq *heathrow_pic_init(MemoryRegion **pmem,
201 3cbee15b j_mayer
                            int nb_cpus, qemu_irq **irqs)
202 e68b9b2b bellard
{
203 e68b9b2b bellard
    HeathrowPICS *s;
204 3b46e624 ths
205 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(HeathrowPICS));
206 3cbee15b j_mayer
    /* only 1 CPU */
207 3cbee15b j_mayer
    s->irqs = irqs[0];
208 23c5e4ca Avi Kivity
    memory_region_init_io(&s->mem, &heathrow_pic_ops, s,
209 23c5e4ca Avi Kivity
                          "heathrow-pic", 0x1000);
210 23c5e4ca Avi Kivity
    *pmem = &s->mem;
211 3cbee15b j_mayer
212 4acd38ce Juan Quintela
    vmstate_register(NULL, -1, &vmstate_heathrow_pic, s);
213 a08d4367 Jan Kiszka
    qemu_register_reset(heathrow_pic_reset, s);
214 d537cf6c pbrook
    return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);
215 e68b9b2b bellard
}