Statistics
| Branch: | Revision:

root / hw / grackle_pci.c @ dde8bbb4

History | View | Annotate | Download (7.7 kB)

1 502a5395 pbrook
/*
2 3cbee15b j_mayer
 * QEMU Grackle PCI host (heathrow OldWorld PowerMac)
3 502a5395 pbrook
 *
4 3cbee15b j_mayer
 * Copyright (c) 2006-2007 Fabrice Bellard
5 3cbee15b j_mayer
 * Copyright (c) 2007 Jocelyn Mayer
6 5fafdf24 ths
 *
7 502a5395 pbrook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 502a5395 pbrook
 * of this software and associated documentation files (the "Software"), to deal
9 502a5395 pbrook
 * in the Software without restriction, including without limitation the rights
10 502a5395 pbrook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 502a5395 pbrook
 * copies of the Software, and to permit persons to whom the Software is
12 502a5395 pbrook
 * furnished to do so, subject to the following conditions:
13 502a5395 pbrook
 *
14 502a5395 pbrook
 * The above copyright notice and this permission notice shall be included in
15 502a5395 pbrook
 * all copies or substantial portions of the Software.
16 502a5395 pbrook
 *
17 502a5395 pbrook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 502a5395 pbrook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 502a5395 pbrook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 502a5395 pbrook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 502a5395 pbrook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 502a5395 pbrook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 502a5395 pbrook
 * THE SOFTWARE.
24 502a5395 pbrook
 */
25 502a5395 pbrook
26 426f17bb Blue Swirl
#include "sysbus.h"
27 3cbee15b j_mayer
#include "ppc_mac.h"
28 87ecb68b pbrook
#include "pci.h"
29 87ecb68b pbrook
30 ea026b2f blueswir1
/* debug Grackle */
31 ea026b2f blueswir1
//#define DEBUG_GRACKLE
32 ea026b2f blueswir1
33 ea026b2f blueswir1
#ifdef DEBUG_GRACKLE
34 001faf32 Blue Swirl
#define GRACKLE_DPRINTF(fmt, ...)                               \
35 001faf32 Blue Swirl
    do { printf("GRACKLE: " fmt , ## __VA_ARGS__); } while (0)
36 ea026b2f blueswir1
#else
37 001faf32 Blue Swirl
#define GRACKLE_DPRINTF(fmt, ...)
38 ea026b2f blueswir1
#endif
39 ea026b2f blueswir1
40 c227f099 Anthony Liguori
typedef target_phys_addr_t pci_addr_t;
41 502a5395 pbrook
#include "pci_host.h"
42 502a5395 pbrook
43 426f17bb Blue Swirl
typedef struct GrackleState {
44 426f17bb Blue Swirl
    SysBusDevice busdev;
45 426f17bb Blue Swirl
    PCIHostState host_state;
46 426f17bb Blue Swirl
} GrackleState;
47 502a5395 pbrook
48 c227f099 Anthony Liguori
static void pci_grackle_config_writel (void *opaque, target_phys_addr_t addr,
49 502a5395 pbrook
                                       uint32_t val)
50 502a5395 pbrook
{
51 502a5395 pbrook
    GrackleState *s = opaque;
52 ea026b2f blueswir1
53 ea026b2f blueswir1
    GRACKLE_DPRINTF("config_writel addr " TARGET_FMT_plx " val %x\n", addr,
54 ea026b2f blueswir1
                    val);
55 502a5395 pbrook
#ifdef TARGET_WORDS_BIGENDIAN
56 502a5395 pbrook
    val = bswap32(val);
57 502a5395 pbrook
#endif
58 426f17bb Blue Swirl
    s->host_state.config_reg = val;
59 502a5395 pbrook
}
60 502a5395 pbrook
61 c227f099 Anthony Liguori
static uint32_t pci_grackle_config_readl (void *opaque, target_phys_addr_t addr)
62 502a5395 pbrook
{
63 502a5395 pbrook
    GrackleState *s = opaque;
64 502a5395 pbrook
    uint32_t val;
65 502a5395 pbrook
66 426f17bb Blue Swirl
    val = s->host_state.config_reg;
67 502a5395 pbrook
#ifdef TARGET_WORDS_BIGENDIAN
68 502a5395 pbrook
    val = bswap32(val);
69 502a5395 pbrook
#endif
70 ea026b2f blueswir1
    GRACKLE_DPRINTF("config_readl addr " TARGET_FMT_plx " val %x\n", addr,
71 ea026b2f blueswir1
                    val);
72 502a5395 pbrook
    return val;
73 502a5395 pbrook
}
74 502a5395 pbrook
75 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const pci_grackle_config_write[] = {
76 502a5395 pbrook
    &pci_grackle_config_writel,
77 502a5395 pbrook
    &pci_grackle_config_writel,
78 502a5395 pbrook
    &pci_grackle_config_writel,
79 502a5395 pbrook
};
80 502a5395 pbrook
81 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const pci_grackle_config_read[] = {
82 502a5395 pbrook
    &pci_grackle_config_readl,
83 502a5395 pbrook
    &pci_grackle_config_readl,
84 502a5395 pbrook
    &pci_grackle_config_readl,
85 502a5395 pbrook
};
86 502a5395 pbrook
87 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const pci_grackle_write[] = {
88 502a5395 pbrook
    &pci_host_data_writeb,
89 502a5395 pbrook
    &pci_host_data_writew,
90 502a5395 pbrook
    &pci_host_data_writel,
91 502a5395 pbrook
};
92 502a5395 pbrook
93 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const pci_grackle_read[] = {
94 502a5395 pbrook
    &pci_host_data_readb,
95 502a5395 pbrook
    &pci_host_data_readw,
96 502a5395 pbrook
    &pci_host_data_readl,
97 502a5395 pbrook
};
98 502a5395 pbrook
99 d2b59317 pbrook
/* Don't know if this matches real hardware, but it agrees with OHW.  */
100 d2b59317 pbrook
static int pci_grackle_map_irq(PCIDevice *pci_dev, int irq_num)
101 502a5395 pbrook
{
102 d2b59317 pbrook
    return (irq_num + (pci_dev->devfn >> 3)) & 3;
103 d2b59317 pbrook
}
104 d2b59317 pbrook
105 5d4e84c8 Juan Quintela
static void pci_grackle_set_irq(void *opaque, int irq_num, int level)
106 d2b59317 pbrook
{
107 5d4e84c8 Juan Quintela
    qemu_irq *pic = opaque;
108 5d4e84c8 Juan Quintela
109 ea026b2f blueswir1
    GRACKLE_DPRINTF("set_irq num %d level %d\n", irq_num, level);
110 3cbee15b j_mayer
    qemu_set_irq(pic[irq_num + 0x15], level);
111 502a5395 pbrook
}
112 502a5395 pbrook
113 9b64997f blueswir1
static void pci_grackle_save(QEMUFile* f, void *opaque)
114 9b64997f blueswir1
{
115 9b64997f blueswir1
    PCIDevice *d = opaque;
116 9b64997f blueswir1
117 9b64997f blueswir1
    pci_device_save(d, f);
118 9b64997f blueswir1
}
119 9b64997f blueswir1
120 9b64997f blueswir1
static int pci_grackle_load(QEMUFile* f, void *opaque, int version_id)
121 9b64997f blueswir1
{
122 9b64997f blueswir1
    PCIDevice *d = opaque;
123 9b64997f blueswir1
124 9b64997f blueswir1
    if (version_id != 1)
125 9b64997f blueswir1
        return -EINVAL;
126 9b64997f blueswir1
127 9b64997f blueswir1
    return pci_device_load(d, f);
128 9b64997f blueswir1
}
129 9b64997f blueswir1
130 6e6b7363 blueswir1
static void pci_grackle_reset(void *opaque)
131 6e6b7363 blueswir1
{
132 6e6b7363 blueswir1
}
133 6e6b7363 blueswir1
134 d537cf6c pbrook
PCIBus *pci_grackle_init(uint32_t base, qemu_irq *pic)
135 502a5395 pbrook
{
136 426f17bb Blue Swirl
    DeviceState *dev;
137 426f17bb Blue Swirl
    SysBusDevice *s;
138 426f17bb Blue Swirl
    GrackleState *d;
139 426f17bb Blue Swirl
140 426f17bb Blue Swirl
    dev = qdev_create(NULL, "grackle");
141 e23a1b33 Markus Armbruster
    qdev_init_nofail(dev);
142 426f17bb Blue Swirl
    s = sysbus_from_qdev(dev);
143 426f17bb Blue Swirl
    d = FROM_SYSBUS(GrackleState, s);
144 cdd0935c Blue Swirl
    d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
145 426f17bb Blue Swirl
                                         pci_grackle_set_irq,
146 426f17bb Blue Swirl
                                         pci_grackle_map_irq,
147 426f17bb Blue Swirl
                                         pic, 0, 4);
148 426f17bb Blue Swirl
149 426f17bb Blue Swirl
    pci_create_simple(d->host_state.bus, 0, "grackle");
150 426f17bb Blue Swirl
151 426f17bb Blue Swirl
    sysbus_mmio_map(s, 0, base);
152 426f17bb Blue Swirl
    sysbus_mmio_map(s, 1, base + 0x00200000);
153 426f17bb Blue Swirl
154 426f17bb Blue Swirl
    return d->host_state.bus;
155 426f17bb Blue Swirl
}
156 426f17bb Blue Swirl
157 81a322d4 Gerd Hoffmann
static int pci_grackle_init_device(SysBusDevice *dev)
158 426f17bb Blue Swirl
{
159 426f17bb Blue Swirl
    GrackleState *s;
160 426f17bb Blue Swirl
    int pci_mem_config, pci_mem_data;
161 426f17bb Blue Swirl
162 426f17bb Blue Swirl
    s = FROM_SYSBUS(GrackleState, dev);
163 426f17bb Blue Swirl
164 426f17bb Blue Swirl
    pci_mem_config = cpu_register_io_memory(pci_grackle_config_read,
165 426f17bb Blue Swirl
                                            pci_grackle_config_write, s);
166 426f17bb Blue Swirl
    pci_mem_data = cpu_register_io_memory(pci_grackle_read,
167 426f17bb Blue Swirl
                                          pci_grackle_write,
168 426f17bb Blue Swirl
                                          &s->host_state);
169 426f17bb Blue Swirl
    sysbus_init_mmio(dev, 0x1000, pci_mem_config);
170 426f17bb Blue Swirl
    sysbus_init_mmio(dev, 0x1000, pci_mem_data);
171 426f17bb Blue Swirl
172 426f17bb Blue Swirl
    register_savevm("grackle", 0, 1, pci_grackle_save, pci_grackle_load,
173 426f17bb Blue Swirl
                    &s->host_state);
174 426f17bb Blue Swirl
    qemu_register_reset(pci_grackle_reset, &s->host_state);
175 426f17bb Blue Swirl
    pci_grackle_reset(&s->host_state);
176 81a322d4 Gerd Hoffmann
    return 0;
177 426f17bb Blue Swirl
}
178 426f17bb Blue Swirl
179 81a322d4 Gerd Hoffmann
static int pci_dec_21154_init_device(SysBusDevice *dev)
180 426f17bb Blue Swirl
{
181 502a5395 pbrook
    GrackleState *s;
182 502a5395 pbrook
    int pci_mem_config, pci_mem_data;
183 502a5395 pbrook
184 426f17bb Blue Swirl
    s = FROM_SYSBUS(GrackleState, dev);
185 502a5395 pbrook
186 1eed09cb Avi Kivity
    pci_mem_config = cpu_register_io_memory(pci_grackle_config_read,
187 502a5395 pbrook
                                            pci_grackle_config_write, s);
188 1eed09cb Avi Kivity
    pci_mem_data = cpu_register_io_memory(pci_grackle_read,
189 426f17bb Blue Swirl
                                          pci_grackle_write,
190 426f17bb Blue Swirl
                                          &s->host_state);
191 426f17bb Blue Swirl
    sysbus_init_mmio(dev, 0x1000, pci_mem_config);
192 426f17bb Blue Swirl
    sysbus_init_mmio(dev, 0x1000, pci_mem_data);
193 81a322d4 Gerd Hoffmann
    return 0;
194 426f17bb Blue Swirl
}
195 426f17bb Blue Swirl
196 81a322d4 Gerd Hoffmann
static int grackle_pci_host_init(PCIDevice *d)
197 426f17bb Blue Swirl
{
198 deb54399 aliguori
    pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_MOTOROLA);
199 deb54399 aliguori
    pci_config_set_device_id(d->config, PCI_DEVICE_ID_MOTOROLA_MPC106);
200 502a5395 pbrook
    d->config[0x08] = 0x00; // revision
201 502a5395 pbrook
    d->config[0x09] = 0x01;
202 173a543b blueswir1
    pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
203 6407f373 Isaku Yamahata
    d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
204 81a322d4 Gerd Hoffmann
    return 0;
205 426f17bb Blue Swirl
}
206 502a5395 pbrook
207 81a322d4 Gerd Hoffmann
static int dec_21154_pci_host_init(PCIDevice *d)
208 426f17bb Blue Swirl
{
209 502a5395 pbrook
    /* PCI2PCI bridge same values as PearPC - check this */
210 4ebcf884 blueswir1
    pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_DEC);
211 4ebcf884 blueswir1
    pci_config_set_device_id(d->config, PCI_DEVICE_ID_DEC_21154);
212 502a5395 pbrook
    d->config[0x08] = 0x02; // revision
213 173a543b blueswir1
    pci_config_set_class(d->config, PCI_CLASS_BRIDGE_PCI);
214 6407f373 Isaku Yamahata
    d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE; // header_type
215 502a5395 pbrook
216 502a5395 pbrook
    d->config[0x18] = 0x0;  // primary_bus
217 502a5395 pbrook
    d->config[0x19] = 0x1;  // secondary_bus
218 502a5395 pbrook
    d->config[0x1a] = 0x1;  // subordinate_bus
219 502a5395 pbrook
    d->config[0x1c] = 0x10; // io_base
220 502a5395 pbrook
    d->config[0x1d] = 0x20; // io_limit
221 3b46e624 ths
222 502a5395 pbrook
    d->config[0x20] = 0x80; // memory_base
223 502a5395 pbrook
    d->config[0x21] = 0x80;
224 502a5395 pbrook
    d->config[0x22] = 0x90; // memory_limit
225 502a5395 pbrook
    d->config[0x23] = 0x80;
226 3b46e624 ths
227 502a5395 pbrook
    d->config[0x24] = 0x00; // prefetchable_memory_base
228 502a5395 pbrook
    d->config[0x25] = 0x84;
229 502a5395 pbrook
    d->config[0x26] = 0x00; // prefetchable_memory_limit
230 502a5395 pbrook
    d->config[0x27] = 0x85;
231 81a322d4 Gerd Hoffmann
    return 0;
232 426f17bb Blue Swirl
}
233 426f17bb Blue Swirl
234 426f17bb Blue Swirl
static PCIDeviceInfo grackle_pci_host_info = {
235 426f17bb Blue Swirl
    .qdev.name = "grackle",
236 426f17bb Blue Swirl
    .qdev.size = sizeof(PCIDevice),
237 426f17bb Blue Swirl
    .init      = grackle_pci_host_init,
238 426f17bb Blue Swirl
};
239 6e6b7363 blueswir1
240 426f17bb Blue Swirl
static PCIDeviceInfo dec_21154_pci_host_info = {
241 426f17bb Blue Swirl
    .qdev.name = "DEC 21154",
242 426f17bb Blue Swirl
    .qdev.size = sizeof(PCIDevice),
243 426f17bb Blue Swirl
    .init      = dec_21154_pci_host_init,
244 426f17bb Blue Swirl
};
245 426f17bb Blue Swirl
246 426f17bb Blue Swirl
static void grackle_register_devices(void)
247 426f17bb Blue Swirl
{
248 426f17bb Blue Swirl
    sysbus_register_dev("grackle", sizeof(GrackleState),
249 426f17bb Blue Swirl
                        pci_grackle_init_device);
250 426f17bb Blue Swirl
    pci_qdev_register(&grackle_pci_host_info);
251 426f17bb Blue Swirl
    sysbus_register_dev("DEC 21154", sizeof(GrackleState),
252 426f17bb Blue Swirl
                        pci_dec_21154_init_device);
253 426f17bb Blue Swirl
    pci_qdev_register(&dec_21154_pci_host_info);
254 502a5395 pbrook
}
255 426f17bb Blue Swirl
256 426f17bb Blue Swirl
device_init(grackle_register_devices)