blob: 7eb0c2bbcb7e68bf8845efa7e28f8a41e52b43c8 [file] [log] [blame]
pbrook502a5392006-05-13 16:11:23 +00001/*
2 * QEMU Ultrasparc APB PCI host
3 *
4 * Copyright (c) 2006 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
pbrook502a5392006-05-13 16:11:23 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook80b3ada2006-09-24 17:01:44 +000024
blueswir1a94fd952009-01-09 20:53:30 +000025/* XXX This file and most of its contents are somewhat misnamed. The
pbrook80b3ada2006-09-24 17:01:44 +000026 Ultrasparc PCI host is called the PCI Bus Module (PBM). The APB is
27 the secondary PCI bridge. */
28
Blue Swirl72f44c82009-07-21 08:36:37 +000029#include "sysbus.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "pci/pci.h"
31#include "pci/pci_host.h"
32#include "pci/pci_bridge.h"
Michael S. Tsirkin06aac7b2012-12-12 15:00:45 +020033#include "pci/pci_bus.h"
Michael S. Tsirkin18e08a52009-11-11 14:59:56 +020034#include "apb_pci.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/address-spaces.h"
blueswir1a94fd952009-01-09 20:53:30 +000037
38/* debug APB */
39//#define DEBUG_APB
40
41#ifdef DEBUG_APB
Blue Swirl001faf32009-05-13 17:53:17 +000042#define APB_DPRINTF(fmt, ...) \
43do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
blueswir1a94fd952009-01-09 20:53:30 +000044#else
Blue Swirl001faf32009-05-13 17:53:17 +000045#define APB_DPRINTF(fmt, ...)
blueswir1a94fd952009-01-09 20:53:30 +000046#endif
47
Blue Swirl930f3fe2009-10-13 18:56:27 +000048/*
49 * Chipset docs:
50 * PBM: "UltraSPARC IIi User's Manual",
51 * http://www.sun.com/processors/manuals/805-0087.pdf
52 *
53 * APB: "Advanced PCI Bridge (APB) User's Manual",
54 * http://www.sun.com/processors/manuals/805-1251.pdf
55 */
56
Blue Swirl95819af2010-01-30 19:48:12 +000057#define PBM_PCI_IMR_MASK 0x7fffffff
58#define PBM_PCI_IMR_ENABLED 0x80000000
59
60#define POR (1 << 31)
61#define SOFT_POR (1 << 30)
62#define SOFT_XIR (1 << 29)
63#define BTN_POR (1 << 28)
64#define BTN_XIR (1 << 27)
65#define RESET_MASK 0xf8000000
66#define RESET_WCMASK 0x98000000
67#define RESET_WMASK 0x60000000
68
Blue Swirl361dea42012-03-10 20:37:00 +000069#define MAX_IVEC 0x30
70
Blue Swirl72f44c82009-07-21 08:36:37 +000071typedef struct APBState {
72 SysBusDevice busdev;
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +040073 PCIBus *bus;
Avi Kivity3812ed02011-08-15 17:17:15 +030074 MemoryRegion apb_config;
75 MemoryRegion pci_config;
Blue Swirlf69539b2011-09-03 16:38:02 +000076 MemoryRegion pci_mmio;
Avi Kivity3812ed02011-08-15 17:17:15 +030077 MemoryRegion pci_ioport;
Blue Swirl95819af2010-01-30 19:48:12 +000078 uint32_t iommu[4];
79 uint32_t pci_control[16];
80 uint32_t pci_irq_map[8];
81 uint32_t obio_irq_map[32];
Blue Swirl361dea42012-03-10 20:37:00 +000082 qemu_irq *pbm_irqs;
83 qemu_irq *ivec_irqs;
Blue Swirl95819af2010-01-30 19:48:12 +000084 uint32_t reset_control;
Blue Swirl9c0afd02010-05-12 19:27:23 +000085 unsigned int nr_resets;
Blue Swirl72f44c82009-07-21 08:36:37 +000086} APBState;
pbrook502a5392006-05-13 16:11:23 +000087
Artyom Tarasenko94d19912012-05-12 11:15:23 +020088static void pci_apb_set_irq(void *opaque, int irq_num, int level);
89
Avi Kivitya8170e52012-10-23 12:30:10 +020090static void apb_config_writel (void *opaque, hwaddr addr,
Avi Kivity3812ed02011-08-15 17:17:15 +030091 uint64_t val, unsigned size)
pbrook502a5392006-05-13 16:11:23 +000092{
Blue Swirl95819af2010-01-30 19:48:12 +000093 APBState *s = opaque;
pbrook502a5392006-05-13 16:11:23 +000094
Blue Swirl361dea42012-03-10 20:37:00 +000095 APB_DPRINTF("%s: addr " TARGET_FMT_lx " val %" PRIx64 "\n", __func__, addr, val);
Blue Swirl95819af2010-01-30 19:48:12 +000096
97 switch (addr & 0xffff) {
98 case 0x30 ... 0x4f: /* DMA error registers */
99 /* XXX: not implemented yet */
100 break;
101 case 0x200 ... 0x20b: /* IOMMU */
102 s->iommu[(addr & 0xf) >> 2] = val;
103 break;
104 case 0x20c ... 0x3ff: /* IOMMU flush */
105 break;
106 case 0xc00 ... 0xc3f: /* PCI interrupt control */
107 if (addr & 4) {
108 s->pci_irq_map[(addr & 0x3f) >> 3] &= PBM_PCI_IMR_MASK;
109 s->pci_irq_map[(addr & 0x3f) >> 3] |= val & ~PBM_PCI_IMR_MASK;
110 }
111 break;
Blue Swirl361dea42012-03-10 20:37:00 +0000112 case 0x1000 ... 0x1080: /* OBIO interrupt control */
113 if (addr & 4) {
114 s->obio_irq_map[(addr & 0xff) >> 3] &= PBM_PCI_IMR_MASK;
115 s->obio_irq_map[(addr & 0xff) >> 3] |= val & ~PBM_PCI_IMR_MASK;
116 }
117 break;
Artyom Tarasenko94d19912012-05-12 11:15:23 +0200118 case 0x1400 ... 0x143f: /* PCI interrupt clear */
119 if (addr & 4) {
120 pci_apb_set_irq(s, (addr & 0x3f) >> 3, 0);
121 }
122 break;
123 case 0x1800 ... 0x1860: /* OBIO interrupt clear */
124 if (addr & 4) {
125 pci_apb_set_irq(s, 0x20 | ((addr & 0xff) >> 3), 0);
126 }
127 break;
Blue Swirl95819af2010-01-30 19:48:12 +0000128 case 0x2000 ... 0x202f: /* PCI control */
129 s->pci_control[(addr & 0x3f) >> 2] = val;
130 break;
131 case 0xf020 ... 0xf027: /* Reset control */
132 if (addr & 4) {
133 val &= RESET_MASK;
134 s->reset_control &= ~(val & RESET_WCMASK);
135 s->reset_control |= val & RESET_WMASK;
136 if (val & SOFT_POR) {
Blue Swirl9c0afd02010-05-12 19:27:23 +0000137 s->nr_resets = 0;
Blue Swirl95819af2010-01-30 19:48:12 +0000138 qemu_system_reset_request();
139 } else if (val & SOFT_XIR) {
140 qemu_system_reset_request();
141 }
142 }
143 break;
144 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
145 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
146 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
147 case 0xf000 ... 0xf01f: /* FFB config, memory control */
148 /* we don't care */
pbrook502a5392006-05-13 16:11:23 +0000149 default:
blueswir1f930d072007-10-06 11:28:21 +0000150 break;
pbrook502a5392006-05-13 16:11:23 +0000151 }
152}
153
Avi Kivity3812ed02011-08-15 17:17:15 +0300154static uint64_t apb_config_readl (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200155 hwaddr addr, unsigned size)
pbrook502a5392006-05-13 16:11:23 +0000156{
Blue Swirl95819af2010-01-30 19:48:12 +0000157 APBState *s = opaque;
pbrook502a5392006-05-13 16:11:23 +0000158 uint32_t val;
159
Blue Swirl95819af2010-01-30 19:48:12 +0000160 switch (addr & 0xffff) {
161 case 0x30 ... 0x4f: /* DMA error registers */
162 val = 0;
163 /* XXX: not implemented yet */
164 break;
165 case 0x200 ... 0x20b: /* IOMMU */
166 val = s->iommu[(addr & 0xf) >> 2];
167 break;
168 case 0x20c ... 0x3ff: /* IOMMU flush */
169 val = 0;
170 break;
171 case 0xc00 ... 0xc3f: /* PCI interrupt control */
172 if (addr & 4) {
173 val = s->pci_irq_map[(addr & 0x3f) >> 3];
174 } else {
175 val = 0;
176 }
177 break;
Blue Swirl361dea42012-03-10 20:37:00 +0000178 case 0x1000 ... 0x1080: /* OBIO interrupt control */
179 if (addr & 4) {
180 val = s->obio_irq_map[(addr & 0xff) >> 3];
181 } else {
182 val = 0;
183 }
184 break;
Blue Swirl95819af2010-01-30 19:48:12 +0000185 case 0x2000 ... 0x202f: /* PCI control */
186 val = s->pci_control[(addr & 0x3f) >> 2];
187 break;
188 case 0xf020 ... 0xf027: /* Reset control */
189 if (addr & 4) {
190 val = s->reset_control;
191 } else {
192 val = 0;
193 }
194 break;
195 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
196 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
197 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
198 case 0xf000 ... 0xf01f: /* FFB config, memory control */
199 /* we don't care */
pbrook502a5392006-05-13 16:11:23 +0000200 default:
blueswir1f930d072007-10-06 11:28:21 +0000201 val = 0;
202 break;
pbrook502a5392006-05-13 16:11:23 +0000203 }
Blue Swirl95819af2010-01-30 19:48:12 +0000204 APB_DPRINTF("%s: addr " TARGET_FMT_lx " -> %x\n", __func__, addr, val);
205
pbrook502a5392006-05-13 16:11:23 +0000206 return val;
207}
208
Avi Kivity3812ed02011-08-15 17:17:15 +0300209static const MemoryRegionOps apb_config_ops = {
210 .read = apb_config_readl,
211 .write = apb_config_writel,
212 .endianness = DEVICE_NATIVE_ENDIAN,
pbrook502a5392006-05-13 16:11:23 +0000213};
214
Avi Kivitya8170e52012-10-23 12:30:10 +0200215static void apb_pci_config_write(void *opaque, hwaddr addr,
Avi Kivity3812ed02011-08-15 17:17:15 +0300216 uint64_t val, unsigned size)
Blue Swirl5a5d4a72010-01-11 21:20:53 +0000217{
Avi Kivity3812ed02011-08-15 17:17:15 +0300218 APBState *s = opaque;
Michael S. Tsirkin63e6f312010-02-22 12:38:25 +0200219
220 val = qemu_bswap_len(val, size);
Blue Swirl361dea42012-03-10 20:37:00 +0000221 APB_DPRINTF("%s: addr " TARGET_FMT_lx " val %" PRIx64 "\n", __func__, addr, val);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400222 pci_data_write(s->bus, addr, val, size);
Blue Swirl5a5d4a72010-01-11 21:20:53 +0000223}
224
Avi Kivitya8170e52012-10-23 12:30:10 +0200225static uint64_t apb_pci_config_read(void *opaque, hwaddr addr,
Avi Kivity3812ed02011-08-15 17:17:15 +0300226 unsigned size)
Blue Swirl5a5d4a72010-01-11 21:20:53 +0000227{
228 uint32_t ret;
Avi Kivity3812ed02011-08-15 17:17:15 +0300229 APBState *s = opaque;
Blue Swirl5a5d4a72010-01-11 21:20:53 +0000230
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400231 ret = pci_data_read(s->bus, addr, size);
Michael S. Tsirkin63e6f312010-02-22 12:38:25 +0200232 ret = qemu_bswap_len(ret, size);
Blue Swirl5a5d4a72010-01-11 21:20:53 +0000233 APB_DPRINTF("%s: addr " TARGET_FMT_lx " -> %x\n", __func__, addr, ret);
234 return ret;
235}
236
Avi Kivitya8170e52012-10-23 12:30:10 +0200237static void pci_apb_iowriteb (void *opaque, hwaddr addr,
pbrook502a5392006-05-13 16:11:23 +0000238 uint32_t val)
239{
Blue Swirlafcea8c2009-09-20 16:05:47 +0000240 cpu_outb(addr & IOPORTS_MASK, val);
pbrook502a5392006-05-13 16:11:23 +0000241}
242
Avi Kivitya8170e52012-10-23 12:30:10 +0200243static void pci_apb_iowritew (void *opaque, hwaddr addr,
pbrook502a5392006-05-13 16:11:23 +0000244 uint32_t val)
245{
Blue Swirla4d5f622010-01-29 18:15:21 +0000246 cpu_outw(addr & IOPORTS_MASK, bswap16(val));
pbrook502a5392006-05-13 16:11:23 +0000247}
248
Avi Kivitya8170e52012-10-23 12:30:10 +0200249static void pci_apb_iowritel (void *opaque, hwaddr addr,
pbrook502a5392006-05-13 16:11:23 +0000250 uint32_t val)
251{
Blue Swirla4d5f622010-01-29 18:15:21 +0000252 cpu_outl(addr & IOPORTS_MASK, bswap32(val));
pbrook502a5392006-05-13 16:11:23 +0000253}
254
Avi Kivitya8170e52012-10-23 12:30:10 +0200255static uint32_t pci_apb_ioreadb (void *opaque, hwaddr addr)
pbrook502a5392006-05-13 16:11:23 +0000256{
257 uint32_t val;
258
Blue Swirlafcea8c2009-09-20 16:05:47 +0000259 val = cpu_inb(addr & IOPORTS_MASK);
pbrook502a5392006-05-13 16:11:23 +0000260 return val;
261}
262
Avi Kivitya8170e52012-10-23 12:30:10 +0200263static uint32_t pci_apb_ioreadw (void *opaque, hwaddr addr)
pbrook502a5392006-05-13 16:11:23 +0000264{
265 uint32_t val;
266
Blue Swirla4d5f622010-01-29 18:15:21 +0000267 val = bswap16(cpu_inw(addr & IOPORTS_MASK));
pbrook502a5392006-05-13 16:11:23 +0000268 return val;
269}
270
Avi Kivitya8170e52012-10-23 12:30:10 +0200271static uint32_t pci_apb_ioreadl (void *opaque, hwaddr addr)
pbrook502a5392006-05-13 16:11:23 +0000272{
273 uint32_t val;
274
Blue Swirla4d5f622010-01-29 18:15:21 +0000275 val = bswap32(cpu_inl(addr & IOPORTS_MASK));
pbrook502a5392006-05-13 16:11:23 +0000276 return val;
277}
278
Avi Kivity3812ed02011-08-15 17:17:15 +0300279static const MemoryRegionOps pci_ioport_ops = {
280 .old_mmio = {
281 .read = { pci_apb_ioreadb, pci_apb_ioreadw, pci_apb_ioreadl },
282 .write = { pci_apb_iowriteb, pci_apb_iowritew, pci_apb_iowritel, },
283 },
284 .endianness = DEVICE_NATIVE_ENDIAN,
pbrook502a5392006-05-13 16:11:23 +0000285};
286
pbrook80b3ada2006-09-24 17:01:44 +0000287/* The APB host has an IRQ line for each IRQ line of each slot. */
pbrookd2b59312006-09-24 00:16:34 +0000288static int pci_apb_map_irq(PCIDevice *pci_dev, int irq_num)
pbrook502a5392006-05-13 16:11:23 +0000289{
pbrook80b3ada2006-09-24 17:01:44 +0000290 return ((pci_dev->devfn & 0x18) >> 1) + irq_num;
291}
292
293static int pci_pbm_map_irq(PCIDevice *pci_dev, int irq_num)
294{
295 int bus_offset;
296 if (pci_dev->devfn & 1)
297 bus_offset = 16;
298 else
299 bus_offset = 0;
300 return bus_offset + irq_num;
pbrookd2b59312006-09-24 00:16:34 +0000301}
302
Juan Quintela5d4e84c2009-08-28 15:28:17 +0200303static void pci_apb_set_irq(void *opaque, int irq_num, int level)
pbrookd2b59312006-09-24 00:16:34 +0000304{
Blue Swirl95819af2010-01-30 19:48:12 +0000305 APBState *s = opaque;
Juan Quintela5d4e84c2009-08-28 15:28:17 +0200306
pbrook80b3ada2006-09-24 17:01:44 +0000307 /* PCI IRQ map onto the first 32 INO. */
Blue Swirl95819af2010-01-30 19:48:12 +0000308 if (irq_num < 32) {
309 if (s->pci_irq_map[irq_num >> 2] & PBM_PCI_IMR_ENABLED) {
310 APB_DPRINTF("%s: set irq %d level %d\n", __func__, irq_num, level);
Blue Swirl361dea42012-03-10 20:37:00 +0000311 qemu_set_irq(s->ivec_irqs[irq_num], level);
Blue Swirl95819af2010-01-30 19:48:12 +0000312 } else {
313 APB_DPRINTF("%s: not enabled: lower irq %d\n", __func__, irq_num);
Blue Swirl361dea42012-03-10 20:37:00 +0000314 qemu_irq_lower(s->ivec_irqs[irq_num]);
315 }
316 } else {
317 /* OBIO IRQ map onto the next 16 INO. */
318 if (s->obio_irq_map[irq_num - 32] & PBM_PCI_IMR_ENABLED) {
319 APB_DPRINTF("%s: set irq %d level %d\n", __func__, irq_num, level);
320 qemu_set_irq(s->ivec_irqs[irq_num], level);
321 } else {
322 APB_DPRINTF("%s: not enabled: lower irq %d\n", __func__, irq_num);
323 qemu_irq_lower(s->ivec_irqs[irq_num]);
Blue Swirl95819af2010-01-30 19:48:12 +0000324 }
325 }
pbrook502a5392006-05-13 16:11:23 +0000326}
327
Isaku Yamahata68f79992010-07-13 13:01:42 +0900328static int apb_pci_bridge_initfn(PCIDevice *dev)
Michael S. Tsirkind6318732009-11-11 14:33:54 +0200329{
Isaku Yamahata68f79992010-07-13 13:01:42 +0900330 int rc;
331
332 rc = pci_bridge_initfn(dev);
333 if (rc < 0) {
334 return rc;
335 }
336
Michael S. Tsirkind6318732009-11-11 14:33:54 +0200337 /*
338 * command register:
339 * According to PCI bridge spec, after reset
340 * bus master bit is off
341 * memory space enable bit is off
342 * According to manual (805-1251.pdf).
343 * the reset value should be zero unless the boot pin is tied high
344 * (which is true) and thus it should be PCI_COMMAND_MEMORY.
345 */
346 pci_set_word(dev->config + PCI_COMMAND,
Blue Swirl9fe52c72010-02-14 08:27:19 +0000347 PCI_COMMAND_MEMORY);
348 pci_set_word(dev->config + PCI_STATUS,
349 PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
350 PCI_STATUS_DEVSEL_MEDIUM);
Isaku Yamahata68f79992010-07-13 13:01:42 +0900351 return 0;
Michael S. Tsirkind6318732009-11-11 14:33:54 +0200352}
353
Avi Kivitya8170e52012-10-23 12:30:10 +0200354PCIBus *pci_apb_init(hwaddr special_base,
355 hwaddr mem_base,
Blue Swirl361dea42012-03-10 20:37:00 +0000356 qemu_irq *ivec_irqs, PCIBus **bus2, PCIBus **bus3,
357 qemu_irq **pbm_irqs)
pbrook502a5392006-05-13 16:11:23 +0000358{
Blue Swirl72f44c82009-07-21 08:36:37 +0000359 DeviceState *dev;
360 SysBusDevice *s;
361 APBState *d;
Isaku Yamahata68f79992010-07-13 13:01:42 +0900362 PCIDevice *pci_dev;
363 PCIBridge *br;
Blue Swirl72f44c82009-07-21 08:36:37 +0000364
365 /* Ultrasparc PBM main bus */
366 dev = qdev_create(NULL, "pbm");
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200367 qdev_init_nofail(dev);
Andreas Färber1356b982013-01-20 02:47:33 +0100368 s = SYS_BUS_DEVICE(dev);
Blue Swirl72f44c82009-07-21 08:36:37 +0000369 /* apb_config */
Blue Swirlbae7b512010-01-10 18:25:48 +0000370 sysbus_mmio_map(s, 0, special_base);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400371 /* PCI configuration space */
372 sysbus_mmio_map(s, 1, special_base + 0x1000000ULL);
Blue Swirl72f44c82009-07-21 08:36:37 +0000373 /* pci_ioport */
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400374 sysbus_mmio_map(s, 2, special_base + 0x2000000ULL);
Blue Swirl72f44c82009-07-21 08:36:37 +0000375 d = FROM_SYSBUS(APBState, s);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400376
Blue Swirlf69539b2011-09-03 16:38:02 +0000377 memory_region_init(&d->pci_mmio, "pci-mmio", 0x100000000ULL);
378 memory_region_add_subregion(get_system_memory(), mem_base, &d->pci_mmio);
379
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400380 d->bus = pci_register_bus(&d->busdev.qdev, "pci",
Blue Swirlf69539b2011-09-03 16:38:02 +0000381 pci_apb_set_irq, pci_pbm_map_irq, d,
382 &d->pci_mmio,
383 get_system_io(),
384 0, 32);
Blue Swirlf6b6f1b2009-12-27 20:52:39 +0000385
Blue Swirl361dea42012-03-10 20:37:00 +0000386 *pbm_irqs = d->pbm_irqs;
387 d->ivec_irqs = ivec_irqs;
Blue Swirl95819af2010-01-30 19:48:12 +0000388
Anthony Liguori73093352012-01-25 13:37:36 -0600389 pci_create_simple(d->bus, 0, "pbm-pci");
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400390
Blue Swirl72f44c82009-07-21 08:36:37 +0000391 /* APB secondary busses */
Isaku Yamahata68f79992010-07-13 13:01:42 +0900392 pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true,
393 "pbm-bridge");
394 br = DO_UPCAST(PCIBridge, dev, pci_dev);
395 pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
396 pci_apb_map_irq);
397 qdev_init_nofail(&pci_dev->qdev);
398 *bus2 = pci_bridge_get_sec_bus(br);
Michael S. Tsirkind6318732009-11-11 14:33:54 +0200399
Isaku Yamahata68f79992010-07-13 13:01:42 +0900400 pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
401 "pbm-bridge");
402 br = DO_UPCAST(PCIBridge, dev, pci_dev);
403 pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
404 pci_apb_map_irq);
405 qdev_init_nofail(&pci_dev->qdev);
406 *bus3 = pci_bridge_get_sec_bus(br);
Blue Swirl72f44c82009-07-21 08:36:37 +0000407
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400408 return d->bus;
Blue Swirl72f44c82009-07-21 08:36:37 +0000409}
410
Blue Swirl95819af2010-01-30 19:48:12 +0000411static void pci_pbm_reset(DeviceState *d)
412{
413 unsigned int i;
414 APBState *s = container_of(d, APBState, busdev.qdev);
415
416 for (i = 0; i < 8; i++) {
417 s->pci_irq_map[i] &= PBM_PCI_IMR_MASK;
418 }
Artyom Tarasenkod1d80052012-05-12 11:15:22 +0200419 for (i = 0; i < 32; i++) {
420 s->obio_irq_map[i] &= PBM_PCI_IMR_MASK;
421 }
Blue Swirl95819af2010-01-30 19:48:12 +0000422
Blue Swirl9c0afd02010-05-12 19:27:23 +0000423 if (s->nr_resets++ == 0) {
Blue Swirl95819af2010-01-30 19:48:12 +0000424 /* Power on reset */
425 s->reset_control = POR;
426 }
427}
428
Avi Kivity3812ed02011-08-15 17:17:15 +0300429static const MemoryRegionOps pci_config_ops = {
430 .read = apb_pci_config_read,
431 .write = apb_pci_config_write,
432 .endianness = DEVICE_NATIVE_ENDIAN,
433};
434
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200435static int pci_pbm_init_device(SysBusDevice *dev)
Blue Swirl72f44c82009-07-21 08:36:37 +0000436{
pbrook502a5392006-05-13 16:11:23 +0000437 APBState *s;
Blue Swirl95819af2010-01-30 19:48:12 +0000438 unsigned int i;
pbrook502a5392006-05-13 16:11:23 +0000439
Blue Swirl72f44c82009-07-21 08:36:37 +0000440 s = FROM_SYSBUS(APBState, dev);
Blue Swirl95819af2010-01-30 19:48:12 +0000441 for (i = 0; i < 8; i++) {
442 s->pci_irq_map[i] = (0x1f << 6) | (i << 2);
443 }
Artyom Tarasenkod1d80052012-05-12 11:15:22 +0200444 for (i = 0; i < 32; i++) {
445 s->obio_irq_map[i] = ((0x1f << 6) | 0x20) + i;
446 }
Blue Swirl361dea42012-03-10 20:37:00 +0000447 s->pbm_irqs = qemu_allocate_irqs(pci_apb_set_irq, s, MAX_IVEC);
Blue Swirl95819af2010-01-30 19:48:12 +0000448
Blue Swirl72f44c82009-07-21 08:36:37 +0000449 /* apb_config */
Avi Kivity3812ed02011-08-15 17:17:15 +0300450 memory_region_init_io(&s->apb_config, &apb_config_ops, s, "apb-config",
451 0x10000);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400452 /* at region 0 */
Avi Kivity750ecd42011-11-27 11:38:10 +0200453 sysbus_init_mmio(dev, &s->apb_config);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400454
Avi Kivity3812ed02011-08-15 17:17:15 +0300455 memory_region_init_io(&s->pci_config, &pci_config_ops, s, "apb-pci-config",
456 0x1000000);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400457 /* at region 1 */
Avi Kivity750ecd42011-11-27 11:38:10 +0200458 sysbus_init_mmio(dev, &s->pci_config);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400459
460 /* pci_ioport */
Avi Kivity3812ed02011-08-15 17:17:15 +0300461 memory_region_init_io(&s->pci_ioport, &pci_ioport_ops, s,
462 "apb-pci-ioport", 0x10000);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400463 /* at region 2 */
Avi Kivity750ecd42011-11-27 11:38:10 +0200464 sysbus_init_mmio(dev, &s->pci_ioport);
Igor V. Kovalenkod63baf92010-05-25 16:09:03 +0400465
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200466 return 0;
Blue Swirl72f44c82009-07-21 08:36:37 +0000467}
pbrook502a5392006-05-13 16:11:23 +0000468
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200469static int pbm_pci_host_init(PCIDevice *d)
Blue Swirl72f44c82009-07-21 08:36:37 +0000470{
Blue Swirl9fe52c72010-02-14 08:27:19 +0000471 pci_set_word(d->config + PCI_COMMAND,
472 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
473 pci_set_word(d->config + PCI_STATUS,
474 PCI_STATUS_FAST_BACK | PCI_STATUS_66MHZ |
475 PCI_STATUS_DEVSEL_MEDIUM);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200476 return 0;
pbrook502a5392006-05-13 16:11:23 +0000477}
Blue Swirl72f44c82009-07-21 08:36:37 +0000478
Anthony Liguori40021f02011-12-04 12:22:06 -0600479static void pbm_pci_host_class_init(ObjectClass *klass, void *data)
480{
481 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
482
483 k->init = pbm_pci_host_init;
484 k->vendor_id = PCI_VENDOR_ID_SUN;
485 k->device_id = PCI_DEVICE_ID_SUN_SABRE;
486 k->class_id = PCI_CLASS_BRIDGE_HOST;
Anthony Liguori40021f02011-12-04 12:22:06 -0600487}
488
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100489static const TypeInfo pbm_pci_host_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600490 .name = "pbm-pci",
491 .parent = TYPE_PCI_DEVICE,
492 .instance_size = sizeof(PCIDevice),
493 .class_init = pbm_pci_host_class_init,
Blue Swirl72f44c82009-07-21 08:36:37 +0000494};
495
Anthony Liguori999e12b2012-01-24 13:12:29 -0600496static void pbm_host_class_init(ObjectClass *klass, void *data)
497{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600498 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600499 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
500
501 k->init = pci_pbm_init_device;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600502 dc->reset = pci_pbm_reset;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600503}
504
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100505static const TypeInfo pbm_host_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600506 .name = "pbm",
507 .parent = TYPE_SYS_BUS_DEVICE,
508 .instance_size = sizeof(APBState),
509 .class_init = pbm_host_class_init,
Blue Swirl95819af2010-01-30 19:48:12 +0000510};
Isaku Yamahata68f79992010-07-13 13:01:42 +0900511
Anthony Liguori40021f02011-12-04 12:22:06 -0600512static void pbm_pci_bridge_class_init(ObjectClass *klass, void *data)
513{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600514 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600515 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
516
517 k->init = apb_pci_bridge_initfn;
518 k->exit = pci_bridge_exitfn;
519 k->vendor_id = PCI_VENDOR_ID_SUN;
520 k->device_id = PCI_DEVICE_ID_SUN_SIMBA;
521 k->revision = 0x11;
522 k->config_write = pci_bridge_write_config;
523 k->is_bridge = 1;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600524 dc->reset = pci_bridge_reset;
525 dc->vmsd = &vmstate_pci_device;
Anthony Liguori40021f02011-12-04 12:22:06 -0600526}
527
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100528static const TypeInfo pbm_pci_bridge_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600529 .name = "pbm-bridge",
530 .parent = TYPE_PCI_DEVICE,
531 .instance_size = sizeof(PCIBridge),
532 .class_init = pbm_pci_bridge_class_init,
Isaku Yamahata68f79992010-07-13 13:01:42 +0900533};
534
Andreas Färber83f7d432012-02-09 15:20:55 +0100535static void pbm_register_types(void)
Blue Swirl72f44c82009-07-21 08:36:37 +0000536{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600537 type_register_static(&pbm_host_info);
538 type_register_static(&pbm_pci_host_info);
539 type_register_static(&pbm_pci_bridge_info);
Blue Swirl72f44c82009-07-21 08:36:37 +0000540}
541
Andreas Färber83f7d432012-02-09 15:20:55 +0100542type_init(pbm_register_types)