blob: 9ff3119edcf02bc725dbdda145bc47cf88ed2b88 [file] [log] [blame]
bellarde68b9b22005-06-05 15:21:57 +00001/*
j_mayer3cbee152007-10-28 23:42:18 +00002 * Heathrow PIC support (OldWorld PowerMac)
ths5fafdf22007-09-16 21:08:06 +00003 *
j_mayer3cbee152007-10-28 23:42:18 +00004 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
ths5fafdf22007-09-16 21:08:06 +00006 *
bellarde68b9b22005-06-05 15:21:57 +00007 * 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 */
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/hw.h"
26#include "hw/ppc/mac.h"
bellarde68b9b22005-06-05 15:21:57 +000027
blueswir1ea026b22008-12-24 09:38:16 +000028/* debug PIC */
29//#define DEBUG_PIC
30
31#ifdef DEBUG_PIC
Blue Swirl001faf32009-05-13 17:53:17 +000032#define PIC_DPRINTF(fmt, ...) \
33 do { printf("PIC: " fmt , ## __VA_ARGS__); } while (0)
blueswir1ea026b22008-12-24 09:38:16 +000034#else
Blue Swirl001faf32009-05-13 17:53:17 +000035#define PIC_DPRINTF(fmt, ...)
blueswir1ea026b22008-12-24 09:38:16 +000036#endif
bellarde68b9b22005-06-05 15:21:57 +000037
38typedef struct HeathrowPIC {
39 uint32_t events;
40 uint32_t mask;
41 uint32_t levels;
42 uint32_t level_triggered;
43} HeathrowPIC;
44
pbrookd537cf62007-04-07 18:14:41 +000045typedef struct HeathrowPICS {
Avi Kivity23c5e4c2011-08-08 16:09:17 +030046 MemoryRegion mem;
bellarde68b9b22005-06-05 15:21:57 +000047 HeathrowPIC pics[2];
j_mayer3cbee152007-10-28 23:42:18 +000048 qemu_irq *irqs;
pbrookd537cf62007-04-07 18:14:41 +000049} HeathrowPICS;
bellarde68b9b22005-06-05 15:21:57 +000050
51static inline int check_irq(HeathrowPIC *pic)
52{
53 return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
54}
55
56/* update the CPU irq state */
57static void heathrow_pic_update(HeathrowPICS *s)
58{
59 if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
j_mayer3cbee152007-10-28 23:42:18 +000060 qemu_irq_raise(s->irqs[0]);
bellarde68b9b22005-06-05 15:21:57 +000061 } else {
j_mayer3cbee152007-10-28 23:42:18 +000062 qemu_irq_lower(s->irqs[0]);
bellarde68b9b22005-06-05 15:21:57 +000063 }
64}
65
Avi Kivitya8170e52012-10-23 12:30:10 +020066static void pic_write(void *opaque, hwaddr addr,
Avi Kivity23c5e4c2011-08-08 16:09:17 +030067 uint64_t value, unsigned size)
bellarde68b9b22005-06-05 15:21:57 +000068{
69 HeathrowPICS *s = opaque;
70 HeathrowPIC *pic;
71 unsigned int n;
72
bellarde68b9b22005-06-05 15:21:57 +000073 n = ((addr & 0xfff) - 0x10) >> 4;
blueswir1ea026b22008-12-24 09:38:16 +000074 PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
bellarde68b9b22005-06-05 15:21:57 +000075 if (n >= 2)
76 return;
77 pic = &s->pics[n];
78 switch(addr & 0xf) {
79 case 0x04:
80 pic->mask = value;
81 heathrow_pic_update(s);
82 break;
83 case 0x08:
84 /* do not reset level triggered IRQs */
85 value &= ~pic->level_triggered;
86 pic->events &= ~value;
87 heathrow_pic_update(s);
88 break;
89 default:
90 break;
91 }
92}
93
Avi Kivitya8170e52012-10-23 12:30:10 +020094static uint64_t pic_read(void *opaque, hwaddr addr,
Avi Kivity23c5e4c2011-08-08 16:09:17 +030095 unsigned size)
bellarde68b9b22005-06-05 15:21:57 +000096{
97 HeathrowPICS *s = opaque;
98 HeathrowPIC *pic;
99 unsigned int n;
100 uint32_t value;
ths3b46e622007-09-17 08:09:54 +0000101
bellarde68b9b22005-06-05 15:21:57 +0000102 n = ((addr & 0xfff) - 0x10) >> 4;
103 if (n >= 2) {
104 value = 0;
105 } else {
106 pic = &s->pics[n];
107 switch(addr & 0xf) {
108 case 0x0:
109 value = pic->events;
110 break;
111 case 0x4:
112 value = pic->mask;
113 break;
114 case 0xc:
115 value = pic->levels;
116 break;
117 default:
118 value = 0;
119 break;
120 }
121 }
blueswir1ea026b22008-12-24 09:38:16 +0000122 PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
bellarde68b9b22005-06-05 15:21:57 +0000123 return value;
124}
125
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300126static const MemoryRegionOps heathrow_pic_ops = {
127 .read = pic_read,
128 .write = pic_write,
Alexander Graf01576442011-09-13 10:41:23 +0200129 .endianness = DEVICE_LITTLE_ENDIAN,
bellarde68b9b22005-06-05 15:21:57 +0000130};
131
pbrookd537cf62007-04-07 18:14:41 +0000132static void heathrow_pic_set_irq(void *opaque, int num, int level)
bellarde68b9b22005-06-05 15:21:57 +0000133{
134 HeathrowPICS *s = opaque;
135 HeathrowPIC *pic;
136 unsigned int irq_bit;
137
138#if defined(DEBUG)
139 {
140 static int last_level[64];
141 if (last_level[num] != level) {
blueswir1ea026b22008-12-24 09:38:16 +0000142 PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
bellarde68b9b22005-06-05 15:21:57 +0000143 last_level[num] = level;
144 }
145 }
146#endif
147 pic = &s->pics[1 - (num >> 5)];
148 irq_bit = 1 << (num & 0x1f);
149 if (level) {
150 pic->events |= irq_bit & ~pic->level_triggered;
151 pic->levels |= irq_bit;
152 } else {
153 pic->levels &= ~irq_bit;
154 }
155 heathrow_pic_update(s);
156}
157
Juan Quintela4acd38c2010-12-02 13:17:23 +0100158static const VMStateDescription vmstate_heathrow_pic_one = {
159 .name = "heathrow_pic_one",
160 .version_id = 0,
161 .minimum_version_id = 0,
Juan Quintela3aff6c22014-04-16 15:24:04 +0200162 .fields = (VMStateField[]) {
Juan Quintela4acd38c2010-12-02 13:17:23 +0100163 VMSTATE_UINT32(events, HeathrowPIC),
164 VMSTATE_UINT32(mask, HeathrowPIC),
165 VMSTATE_UINT32(levels, HeathrowPIC),
166 VMSTATE_UINT32(level_triggered, HeathrowPIC),
167 VMSTATE_END_OF_LIST()
168 }
169};
blueswir19b649972008-12-30 19:01:19 +0000170
Juan Quintela4acd38c2010-12-02 13:17:23 +0100171static const VMStateDescription vmstate_heathrow_pic = {
172 .name = "heathrow_pic",
173 .version_id = 1,
174 .minimum_version_id = 1,
Juan Quintela3aff6c22014-04-16 15:24:04 +0200175 .fields = (VMStateField[]) {
Juan Quintela4acd38c2010-12-02 13:17:23 +0100176 VMSTATE_STRUCT_ARRAY(pics, HeathrowPICS, 2, 1,
177 vmstate_heathrow_pic_one, HeathrowPIC),
178 VMSTATE_END_OF_LIST()
179 }
180};
blueswir19b649972008-12-30 19:01:19 +0000181
blueswir16e6b7362008-12-28 18:27:10 +0000182static void heathrow_pic_reset_one(HeathrowPIC *s)
183{
184 memset(s, '\0', sizeof(HeathrowPIC));
185}
186
187static void heathrow_pic_reset(void *opaque)
188{
189 HeathrowPICS *s = opaque;
190
191 heathrow_pic_reset_one(&s->pics[0]);
192 heathrow_pic_reset_one(&s->pics[1]);
193
194 s->pics[0].level_triggered = 0;
195 s->pics[1].level_triggered = 0x1ff00000;
196}
197
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300198qemu_irq *heathrow_pic_init(MemoryRegion **pmem,
j_mayer3cbee152007-10-28 23:42:18 +0000199 int nb_cpus, qemu_irq **irqs)
bellarde68b9b22005-06-05 15:21:57 +0000200{
201 HeathrowPICS *s;
ths3b46e622007-09-17 08:09:54 +0000202
Anthony Liguori7267c092011-08-20 22:09:37 -0500203 s = g_malloc0(sizeof(HeathrowPICS));
j_mayer3cbee152007-10-28 23:42:18 +0000204 /* only 1 CPU */
205 s->irqs = irqs[0];
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400206 memory_region_init_io(&s->mem, NULL, &heathrow_pic_ops, s,
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300207 "heathrow-pic", 0x1000);
208 *pmem = &s->mem;
j_mayer3cbee152007-10-28 23:42:18 +0000209
Juan Quintela4acd38c2010-12-02 13:17:23 +0100210 vmstate_register(NULL, -1, &vmstate_heathrow_pic, s);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200211 qemu_register_reset(heathrow_pic_reset, s);
pbrookd537cf62007-04-07 18:14:41 +0000212 return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);
bellarde68b9b22005-06-05 15:21:57 +0000213}