2 * QEMU Ultrasparc APB PCI host
4 * Copyright (c) 2006 Fabrice Bellard
6 * 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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* XXX This file and most of its contents are somewhat misnamed. The
26 Ultrasparc PCI host is called the PCI Bus Module (PBM). The APB is
27 the secondary PCI bridge. */
32 #include "rwhandler.h"
40 #define APB_DPRINTF(fmt, ...) \
41 do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
43 #define APB_DPRINTF(fmt, ...)
48 * PBM: "UltraSPARC IIi User's Manual",
49 * http://www.sun.com/processors/manuals/805-0087.pdf
51 * APB: "Advanced PCI Bridge (APB) User's Manual",
52 * http://www.sun.com/processors/manuals/805-1251.pdf
55 #define PBM_PCI_IMR_MASK 0x7fffffff
56 #define PBM_PCI_IMR_ENABLED 0x80000000
59 #define SOFT_POR (1 << 30)
60 #define SOFT_XIR (1 << 29)
61 #define BTN_POR (1 << 28)
62 #define BTN_XIR (1 << 27)
63 #define RESET_MASK 0xf8000000
64 #define RESET_WCMASK 0x98000000
65 #define RESET_WMASK 0x60000000
67 typedef struct APBState
{
70 ReadWriteHandler pci_config_handler
;
72 uint32_t pci_control
[16];
73 uint32_t pci_irq_map
[8];
74 uint32_t obio_irq_map
[32];
75 qemu_irq pci_irqs
[32];
76 uint32_t reset_control
;
77 unsigned int nr_resets
;
80 static void apb_config_writel (void *opaque
, target_phys_addr_t addr
,
85 APB_DPRINTF("%s: addr " TARGET_FMT_lx
" val %x\n", __func__
, addr
, val
);
87 switch (addr
& 0xffff) {
88 case 0x30 ... 0x4f: /* DMA error registers */
89 /* XXX: not implemented yet */
91 case 0x200 ... 0x20b: /* IOMMU */
92 s
->iommu
[(addr
& 0xf) >> 2] = val
;
94 case 0x20c ... 0x3ff: /* IOMMU flush */
96 case 0xc00 ... 0xc3f: /* PCI interrupt control */
98 s
->pci_irq_map
[(addr
& 0x3f) >> 3] &= PBM_PCI_IMR_MASK
;
99 s
->pci_irq_map
[(addr
& 0x3f) >> 3] |= val
& ~PBM_PCI_IMR_MASK
;
102 case 0x2000 ... 0x202f: /* PCI control */
103 s
->pci_control
[(addr
& 0x3f) >> 2] = val
;
105 case 0xf020 ... 0xf027: /* Reset control */
108 s
->reset_control
&= ~(val
& RESET_WCMASK
);
109 s
->reset_control
|= val
& RESET_WMASK
;
110 if (val
& SOFT_POR
) {
112 qemu_system_reset_request();
113 } else if (val
& SOFT_XIR
) {
114 qemu_system_reset_request();
118 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
119 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
120 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
121 case 0xf000 ... 0xf01f: /* FFB config, memory control */
128 static uint32_t apb_config_readl (void *opaque
,
129 target_phys_addr_t addr
)
131 APBState
*s
= opaque
;
134 switch (addr
& 0xffff) {
135 case 0x30 ... 0x4f: /* DMA error registers */
137 /* XXX: not implemented yet */
139 case 0x200 ... 0x20b: /* IOMMU */
140 val
= s
->iommu
[(addr
& 0xf) >> 2];
142 case 0x20c ... 0x3ff: /* IOMMU flush */
145 case 0xc00 ... 0xc3f: /* PCI interrupt control */
147 val
= s
->pci_irq_map
[(addr
& 0x3f) >> 3];
152 case 0x2000 ... 0x202f: /* PCI control */
153 val
= s
->pci_control
[(addr
& 0x3f) >> 2];
155 case 0xf020 ... 0xf027: /* Reset control */
157 val
= s
->reset_control
;
162 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
163 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
164 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
165 case 0xf000 ... 0xf01f: /* FFB config, memory control */
171 APB_DPRINTF("%s: addr " TARGET_FMT_lx
" -> %x\n", __func__
, addr
, val
);
176 static CPUWriteMemoryFunc
* const apb_config_write
[] = {
182 static CPUReadMemoryFunc
* const apb_config_read
[] = {
188 static void apb_pci_config_write(ReadWriteHandler
*h
, pcibus_t addr
,
189 uint32_t val
, int size
)
191 APBState
*s
= container_of(h
, APBState
, pci_config_handler
);
193 val
= qemu_bswap_len(val
, size
);
194 APB_DPRINTF("%s: addr " TARGET_FMT_lx
" val %x\n", __func__
, addr
, val
);
195 pci_data_write(s
->bus
, addr
, val
, size
);
198 static uint32_t apb_pci_config_read(ReadWriteHandler
*h
, pcibus_t addr
,
202 APBState
*s
= container_of(h
, APBState
, pci_config_handler
);
204 ret
= pci_data_read(s
->bus
, addr
, size
);
205 ret
= qemu_bswap_len(ret
, size
);
206 APB_DPRINTF("%s: addr " TARGET_FMT_lx
" -> %x\n", __func__
, addr
, ret
);
210 static void pci_apb_iowriteb (void *opaque
, target_phys_addr_t addr
,
213 cpu_outb(addr
& IOPORTS_MASK
, val
);
216 static void pci_apb_iowritew (void *opaque
, target_phys_addr_t addr
,
219 cpu_outw(addr
& IOPORTS_MASK
, bswap16(val
));
222 static void pci_apb_iowritel (void *opaque
, target_phys_addr_t addr
,
225 cpu_outl(addr
& IOPORTS_MASK
, bswap32(val
));
228 static uint32_t pci_apb_ioreadb (void *opaque
, target_phys_addr_t addr
)
232 val
= cpu_inb(addr
& IOPORTS_MASK
);
236 static uint32_t pci_apb_ioreadw (void *opaque
, target_phys_addr_t addr
)
240 val
= bswap16(cpu_inw(addr
& IOPORTS_MASK
));
244 static uint32_t pci_apb_ioreadl (void *opaque
, target_phys_addr_t addr
)
248 val
= bswap32(cpu_inl(addr
& IOPORTS_MASK
));
252 static CPUWriteMemoryFunc
* const pci_apb_iowrite
[] = {
258 static CPUReadMemoryFunc
* const pci_apb_ioread
[] = {
264 /* The APB host has an IRQ line for each IRQ line of each slot. */
265 static int pci_apb_map_irq(PCIDevice
*pci_dev
, int irq_num
)
267 return ((pci_dev
->devfn
& 0x18) >> 1) + irq_num
;
270 static int pci_pbm_map_irq(PCIDevice
*pci_dev
, int irq_num
)
273 if (pci_dev
->devfn
& 1)
277 return bus_offset
+ irq_num
;
280 static void pci_apb_set_irq(void *opaque
, int irq_num
, int level
)
282 APBState
*s
= opaque
;
284 /* PCI IRQ map onto the first 32 INO. */
286 if (s
->pci_irq_map
[irq_num
>> 2] & PBM_PCI_IMR_ENABLED
) {
287 APB_DPRINTF("%s: set irq %d level %d\n", __func__
, irq_num
, level
);
288 qemu_set_irq(s
->pci_irqs
[irq_num
], level
);
290 APB_DPRINTF("%s: not enabled: lower irq %d\n", __func__
, irq_num
);
291 qemu_irq_lower(s
->pci_irqs
[irq_num
]);
296 static void apb_pci_bridge_init(PCIBus
*b
)
298 PCIDevice
*dev
= pci_bridge_get_device(b
);
302 * According to PCI bridge spec, after reset
303 * bus master bit is off
304 * memory space enable bit is off
305 * According to manual (805-1251.pdf).
306 * the reset value should be zero unless the boot pin is tied high
307 * (which is true) and thus it should be PCI_COMMAND_MEMORY.
309 pci_set_word(dev
->config
+ PCI_COMMAND
,
311 pci_set_word(dev
->config
+ PCI_STATUS
,
312 PCI_STATUS_FAST_BACK
| PCI_STATUS_66MHZ
|
313 PCI_STATUS_DEVSEL_MEDIUM
);
314 pci_set_byte(dev
->config
+ PCI_REVISION_ID
, 0x11);
317 PCIBus
*pci_apb_init(target_phys_addr_t special_base
,
318 target_phys_addr_t mem_base
,
319 qemu_irq
*pic
, PCIBus
**bus2
, PCIBus
**bus3
)
326 /* Ultrasparc PBM main bus */
327 dev
= qdev_create(NULL
, "pbm");
328 qdev_init_nofail(dev
);
329 s
= sysbus_from_qdev(dev
);
331 sysbus_mmio_map(s
, 0, special_base
);
332 /* PCI configuration space */
333 sysbus_mmio_map(s
, 1, special_base
+ 0x1000000ULL
);
335 sysbus_mmio_map(s
, 2, special_base
+ 0x2000000ULL
);
336 d
= FROM_SYSBUS(APBState
, s
);
338 d
->bus
= pci_register_bus(&d
->busdev
.qdev
, "pci",
339 pci_apb_set_irq
, pci_pbm_map_irq
, d
,
341 pci_bus_set_mem_base(d
->bus
, mem_base
);
343 for (i
= 0; i
< 32; i
++) {
344 sysbus_connect_irq(s
, i
, pic
[i
]);
347 pci_create_simple(d
->bus
, 0, "pbm");
349 /* APB secondary busses */
350 *bus2
= pci_bridge_init(d
->bus
, PCI_DEVFN(1, 0), true,
351 PCI_VENDOR_ID_SUN
, PCI_DEVICE_ID_SUN_SIMBA
,
353 "Advanced PCI Bus secondary bridge 1");
354 apb_pci_bridge_init(*bus2
);
356 *bus3
= pci_bridge_init(d
->bus
, PCI_DEVFN(1, 1), true,
357 PCI_VENDOR_ID_SUN
, PCI_DEVICE_ID_SUN_SIMBA
,
359 "Advanced PCI Bus secondary bridge 2");
360 apb_pci_bridge_init(*bus3
);
365 static void pci_pbm_reset(DeviceState
*d
)
368 APBState
*s
= container_of(d
, APBState
, busdev
.qdev
);
370 for (i
= 0; i
< 8; i
++) {
371 s
->pci_irq_map
[i
] &= PBM_PCI_IMR_MASK
;
374 if (s
->nr_resets
++ == 0) {
376 s
->reset_control
= POR
;
380 static int pci_pbm_init_device(SysBusDevice
*dev
)
383 int pci_config
, apb_config
, pci_ioport
;
386 s
= FROM_SYSBUS(APBState
, dev
);
387 for (i
= 0; i
< 8; i
++) {
388 s
->pci_irq_map
[i
] = (0x1f << 6) | (i
<< 2);
390 for (i
= 0; i
< 32; i
++) {
391 sysbus_init_irq(dev
, &s
->pci_irqs
[i
]);
395 apb_config
= cpu_register_io_memory(apb_config_read
,
396 apb_config_write
, s
);
398 sysbus_init_mmio(dev
, 0x10000ULL
, apb_config
);
400 /* PCI configuration space */
401 s
->pci_config_handler
.read
= apb_pci_config_read
;
402 s
->pci_config_handler
.write
= apb_pci_config_write
;
403 pci_config
= cpu_register_io_memory_simple(&s
->pci_config_handler
);
404 assert(pci_config
>= 0);
406 sysbus_init_mmio(dev
, 0x1000000ULL
, pci_config
);
409 pci_ioport
= cpu_register_io_memory(pci_apb_ioread
,
412 sysbus_init_mmio(dev
, 0x10000ULL
, pci_ioport
);
417 static int pbm_pci_host_init(PCIDevice
*d
)
419 pci_config_set_vendor_id(d
->config
, PCI_VENDOR_ID_SUN
);
420 pci_config_set_device_id(d
->config
, PCI_DEVICE_ID_SUN_SABRE
);
421 pci_set_word(d
->config
+ PCI_COMMAND
,
422 PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
423 pci_set_word(d
->config
+ PCI_STATUS
,
424 PCI_STATUS_FAST_BACK
| PCI_STATUS_66MHZ
|
425 PCI_STATUS_DEVSEL_MEDIUM
);
426 pci_config_set_class(d
->config
, PCI_CLASS_BRIDGE_HOST
);
430 static PCIDeviceInfo pbm_pci_host_info
= {
432 .qdev
.size
= sizeof(PCIDevice
),
433 .init
= pbm_pci_host_init
,
437 static SysBusDeviceInfo pbm_host_info
= {
439 .qdev
.size
= sizeof(APBState
),
440 .qdev
.reset
= pci_pbm_reset
,
441 .init
= pci_pbm_init_device
,
443 static void pbm_register_devices(void)
445 sysbus_register_withprop(&pbm_host_info
);
446 pci_qdev_register(&pbm_pci_host_info
);
449 device_init(pbm_register_devices
)