2 * QEMU Ultrasparc APB PCI host
4 * Copyright (c) 2006 Fabrice Bellard
5 * Copyright (c) 2012,2013 Artyom Tarasenko
7 * 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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 /* XXX This file and most of its contents are somewhat misnamed. The
27 Ultrasparc PCI host is called the PCI Bus Module (PBM). The APB is
28 the secondary PCI bridge. */
30 #include "hw/sysbus.h"
31 #include "hw/pci/pci.h"
32 #include "hw/pci/pci_host.h"
33 #include "hw/pci/pci_bridge.h"
34 #include "hw/pci/pci_bus.h"
35 #include "hw/pci-host/apb.h"
36 #include "sysemu/sysemu.h"
37 #include "exec/address-spaces.h"
43 #define APB_DPRINTF(fmt, ...) \
44 do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
46 #define APB_DPRINTF(fmt, ...)
53 #define IOMMU_DPRINTF(fmt, ...) \
54 do { printf("IOMMU: " fmt , ## __VA_ARGS__); } while (0)
56 #define IOMMU_DPRINTF(fmt, ...)
61 * PBM: "UltraSPARC IIi User's Manual",
62 * http://www.sun.com/processors/manuals/805-0087.pdf
64 * APB: "Advanced PCI Bridge (APB) User's Manual",
65 * http://www.sun.com/processors/manuals/805-1251.pdf
68 #define PBM_PCI_IMR_MASK 0x7fffffff
69 #define PBM_PCI_IMR_ENABLED 0x80000000
71 #define POR (1U << 31)
72 #define SOFT_POR (1U << 30)
73 #define SOFT_XIR (1U << 29)
74 #define BTN_POR (1U << 28)
75 #define BTN_XIR (1U << 27)
76 #define RESET_MASK 0xf8000000
77 #define RESET_WCMASK 0x98000000
78 #define RESET_WMASK 0x60000000
81 #define NO_IRQ_REQUEST (MAX_IVEC + 1)
84 #define IOMMU_CTRL 0x0
85 #define IOMMU_BASE 0x8
87 typedef struct IOMMUState
{
88 uint64_t regs
[IOMMU_NREGS
];
91 #define TYPE_APB "pbm"
93 #define APB_DEVICE(obj) \
94 OBJECT_CHECK(APBState, (obj), TYPE_APB)
96 typedef struct APBState
{
97 PCIHostState parent_obj
;
99 MemoryRegion apb_config
;
100 MemoryRegion pci_config
;
101 MemoryRegion pci_mmio
;
102 MemoryRegion pci_ioport
;
105 uint32_t pci_control
[16];
106 uint32_t pci_irq_map
[8];
107 uint32_t obio_irq_map
[32];
110 unsigned int irq_request
;
111 uint32_t reset_control
;
112 unsigned int nr_resets
;
115 static inline void pbm_set_request(APBState
*s
, unsigned int irq_num
)
117 APB_DPRINTF("%s: request irq %d\n", __func__
, irq_num
);
119 s
->irq_request
= irq_num
;
120 qemu_set_irq(s
->ivec_irqs
[irq_num
], 1);
123 static inline void pbm_check_irqs(APBState
*s
)
128 /* Previous request is not acknowledged, resubmit */
129 if (s
->irq_request
!= NO_IRQ_REQUEST
) {
130 pbm_set_request(s
, s
->irq_request
);
133 /* no request pending */
134 if (s
->pci_irq_in
== 0ULL) {
137 for (i
= 0; i
< 32; i
++) {
138 if (s
->pci_irq_in
& (1ULL << i
)) {
139 if (s
->pci_irq_map
[i
>> 2] & PBM_PCI_IMR_ENABLED
) {
140 pbm_set_request(s
, i
);
145 for (i
= 32; i
< 64; i
++) {
146 if (s
->pci_irq_in
& (1ULL << i
)) {
147 if (s
->obio_irq_map
[i
- 32] & PBM_PCI_IMR_ENABLED
) {
148 pbm_set_request(s
, i
);
155 static inline void pbm_clear_request(APBState
*s
, unsigned int irq_num
)
157 APB_DPRINTF("%s: clear request irq %d\n", __func__
, irq_num
);
158 qemu_set_irq(s
->ivec_irqs
[irq_num
], 0);
159 s
->irq_request
= NO_IRQ_REQUEST
;
162 static void iommu_config_write(void *opaque
, hwaddr addr
,
163 uint64_t val
, unsigned size
)
165 IOMMUState
*is
= opaque
;
167 IOMMU_DPRINTF("IOMMU config write: 0x%" HWADDR_PRIx
" val: %" PRIx64
168 " size: %d\n", addr
, val
, size
);
173 is
->regs
[IOMMU_CTRL
>> 3] &= 0xffffffffULL
;
174 is
->regs
[IOMMU_CTRL
>> 3] |= val
<< 32;
176 is
->regs
[IOMMU_CTRL
] = val
;
179 case IOMMU_CTRL
+ 0x4:
180 is
->regs
[IOMMU_CTRL
>> 3] &= 0xffffffff00000000ULL
;
181 is
->regs
[IOMMU_CTRL
>> 3] |= val
& 0xffffffffULL
;
185 is
->regs
[IOMMU_BASE
>> 3] &= 0xffffffffULL
;
186 is
->regs
[IOMMU_BASE
>> 3] |= val
<< 32;
188 is
->regs
[IOMMU_BASE
] = val
;
191 case IOMMU_BASE
+ 0x4:
192 is
->regs
[IOMMU_BASE
>> 3] &= 0xffffffff00000000ULL
;
193 is
->regs
[IOMMU_BASE
>> 3] |= val
& 0xffffffffULL
;
196 qemu_log_mask(LOG_UNIMP
,
197 "apb iommu: Unimplemented register write "
198 "reg 0x%" HWADDR_PRIx
" size 0x%x value 0x%" PRIx64
"\n",
204 static uint64_t iommu_config_read(void *opaque
, hwaddr addr
, unsigned size
)
206 IOMMUState
*is
= opaque
;
212 val
= is
->regs
[IOMMU_CTRL
>> 3] >> 32;
214 val
= is
->regs
[IOMMU_CTRL
>> 3];
217 case IOMMU_CTRL
+ 0x4:
218 val
= is
->regs
[IOMMU_CTRL
>> 3] & 0xffffffffULL
;
222 val
= is
->regs
[IOMMU_BASE
>> 3] >> 32;
224 val
= is
->regs
[IOMMU_BASE
>> 3];
227 case IOMMU_BASE
+ 0x4:
228 val
= is
->regs
[IOMMU_BASE
>> 3] & 0xffffffffULL
;
231 qemu_log_mask(LOG_UNIMP
,
232 "apb iommu: Unimplemented register read "
233 "reg 0x%" HWADDR_PRIx
" size 0x%x\n",
239 IOMMU_DPRINTF("IOMMU config read: 0x%" HWADDR_PRIx
" val: %" PRIx64
240 " size: %d\n", addr
, val
, size
);
245 static void apb_config_writel (void *opaque
, hwaddr addr
,
246 uint64_t val
, unsigned size
)
248 APBState
*s
= opaque
;
249 IOMMUState
*is
= &s
->iommu
;
251 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" val %" PRIx64
"\n", __func__
, addr
, val
);
253 switch (addr
& 0xffff) {
254 case 0x30 ... 0x4f: /* DMA error registers */
255 /* XXX: not implemented yet */
257 case 0x200 ... 0x217: /* IOMMU */
258 iommu_config_write(is
, (addr
& 0xf), val
, size
);
260 case 0xc00 ... 0xc3f: /* PCI interrupt control */
262 unsigned int ino
= (addr
& 0x3f) >> 3;
263 s
->pci_irq_map
[ino
] &= PBM_PCI_IMR_MASK
;
264 s
->pci_irq_map
[ino
] |= val
& ~PBM_PCI_IMR_MASK
;
265 if ((s
->irq_request
== ino
) && !(val
& ~PBM_PCI_IMR_MASK
)) {
266 pbm_clear_request(s
, ino
);
271 case 0x1000 ... 0x1080: /* OBIO interrupt control */
273 unsigned int ino
= ((addr
& 0xff) >> 3);
274 s
->obio_irq_map
[ino
] &= PBM_PCI_IMR_MASK
;
275 s
->obio_irq_map
[ino
] |= val
& ~PBM_PCI_IMR_MASK
;
276 if ((s
->irq_request
== (ino
| 0x20))
277 && !(val
& ~PBM_PCI_IMR_MASK
)) {
278 pbm_clear_request(s
, ino
| 0x20);
283 case 0x1400 ... 0x14ff: /* PCI interrupt clear */
285 unsigned int ino
= (addr
& 0xff) >> 5;
286 if ((s
->irq_request
/ 4) == ino
) {
287 pbm_clear_request(s
, s
->irq_request
);
292 case 0x1800 ... 0x1860: /* OBIO interrupt clear */
294 unsigned int ino
= ((addr
& 0xff) >> 3) | 0x20;
295 if (s
->irq_request
== ino
) {
296 pbm_clear_request(s
, ino
);
301 case 0x2000 ... 0x202f: /* PCI control */
302 s
->pci_control
[(addr
& 0x3f) >> 2] = val
;
304 case 0xf020 ... 0xf027: /* Reset control */
307 s
->reset_control
&= ~(val
& RESET_WCMASK
);
308 s
->reset_control
|= val
& RESET_WMASK
;
309 if (val
& SOFT_POR
) {
311 qemu_system_reset_request();
312 } else if (val
& SOFT_XIR
) {
313 qemu_system_reset_request();
317 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
318 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
319 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
320 case 0xf000 ... 0xf01f: /* FFB config, memory control */
327 static uint64_t apb_config_readl (void *opaque
,
328 hwaddr addr
, unsigned size
)
330 APBState
*s
= opaque
;
331 IOMMUState
*is
= &s
->iommu
;
334 switch (addr
& 0xffff) {
335 case 0x30 ... 0x4f: /* DMA error registers */
337 /* XXX: not implemented yet */
339 case 0x200 ... 0x217: /* IOMMU */
340 val
= iommu_config_read(is
, (addr
& 0xf), size
);
342 case 0xc00 ... 0xc3f: /* PCI interrupt control */
344 val
= s
->pci_irq_map
[(addr
& 0x3f) >> 3];
349 case 0x1000 ... 0x1080: /* OBIO interrupt control */
351 val
= s
->obio_irq_map
[(addr
& 0xff) >> 3];
356 case 0x2000 ... 0x202f: /* PCI control */
357 val
= s
->pci_control
[(addr
& 0x3f) >> 2];
359 case 0xf020 ... 0xf027: /* Reset control */
361 val
= s
->reset_control
;
366 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
367 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
368 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
369 case 0xf000 ... 0xf01f: /* FFB config, memory control */
375 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" -> %x\n", __func__
, addr
, val
);
380 static const MemoryRegionOps apb_config_ops
= {
381 .read
= apb_config_readl
,
382 .write
= apb_config_writel
,
383 .endianness
= DEVICE_NATIVE_ENDIAN
,
386 static void apb_pci_config_write(void *opaque
, hwaddr addr
,
387 uint64_t val
, unsigned size
)
389 APBState
*s
= opaque
;
390 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
392 val
= qemu_bswap_len(val
, size
);
393 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" val %" PRIx64
"\n", __func__
, addr
, val
);
394 pci_data_write(phb
->bus
, addr
, val
, size
);
397 static uint64_t apb_pci_config_read(void *opaque
, hwaddr addr
,
401 APBState
*s
= opaque
;
402 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
404 ret
= pci_data_read(phb
->bus
, addr
, size
);
405 ret
= qemu_bswap_len(ret
, size
);
406 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" -> %x\n", __func__
, addr
, ret
);
410 /* The APB host has an IRQ line for each IRQ line of each slot. */
411 static int pci_apb_map_irq(PCIDevice
*pci_dev
, int irq_num
)
413 return ((pci_dev
->devfn
& 0x18) >> 1) + irq_num
;
416 static int pci_pbm_map_irq(PCIDevice
*pci_dev
, int irq_num
)
419 if (pci_dev
->devfn
& 1)
423 return (bus_offset
+ (PCI_SLOT(pci_dev
->devfn
) << 2) + irq_num
) & 0x1f;
426 static void pci_apb_set_irq(void *opaque
, int irq_num
, int level
)
428 APBState
*s
= opaque
;
430 APB_DPRINTF("%s: set irq_in %d level %d\n", __func__
, irq_num
, level
);
431 /* PCI IRQ map onto the first 32 INO. */
434 s
->pci_irq_in
|= 1ULL << irq_num
;
435 if (s
->pci_irq_map
[irq_num
>> 2] & PBM_PCI_IMR_ENABLED
) {
436 pbm_set_request(s
, irq_num
);
439 s
->pci_irq_in
&= ~(1ULL << irq_num
);
442 /* OBIO IRQ map onto the next 32 INO. */
444 APB_DPRINTF("%s: set irq %d level %d\n", __func__
, irq_num
, level
);
445 s
->pci_irq_in
|= 1ULL << irq_num
;
446 if ((s
->irq_request
== NO_IRQ_REQUEST
)
447 && (s
->obio_irq_map
[irq_num
- 32] & PBM_PCI_IMR_ENABLED
)) {
448 pbm_set_request(s
, irq_num
);
451 s
->pci_irq_in
&= ~(1ULL << irq_num
);
456 static int apb_pci_bridge_initfn(PCIDevice
*dev
)
460 rc
= pci_bridge_initfn(dev
, TYPE_PCI_BUS
);
467 * According to PCI bridge spec, after reset
468 * bus master bit is off
469 * memory space enable bit is off
470 * According to manual (805-1251.pdf).
471 * the reset value should be zero unless the boot pin is tied high
472 * (which is true) and thus it should be PCI_COMMAND_MEMORY.
474 pci_set_word(dev
->config
+ PCI_COMMAND
,
476 pci_set_word(dev
->config
+ PCI_STATUS
,
477 PCI_STATUS_FAST_BACK
| PCI_STATUS_66MHZ
|
478 PCI_STATUS_DEVSEL_MEDIUM
);
482 PCIBus
*pci_apb_init(hwaddr special_base
,
484 qemu_irq
*ivec_irqs
, PCIBus
**bus2
, PCIBus
**bus3
,
495 /* Ultrasparc PBM main bus */
496 dev
= qdev_create(NULL
, TYPE_APB
);
497 qdev_init_nofail(dev
);
498 s
= SYS_BUS_DEVICE(dev
);
500 sysbus_mmio_map(s
, 0, special_base
);
501 /* PCI configuration space */
502 sysbus_mmio_map(s
, 1, special_base
+ 0x1000000ULL
);
504 sysbus_mmio_map(s
, 2, special_base
+ 0x2000000ULL
);
507 memory_region_init(&d
->pci_mmio
, OBJECT(s
), "pci-mmio", 0x100000000ULL
);
508 memory_region_add_subregion(get_system_memory(), mem_base
, &d
->pci_mmio
);
510 phb
= PCI_HOST_BRIDGE(dev
);
511 phb
->bus
= pci_register_bus(DEVICE(phb
), "pci",
512 pci_apb_set_irq
, pci_pbm_map_irq
, d
,
515 0, 32, TYPE_PCI_BUS
);
517 *pbm_irqs
= d
->pbm_irqs
;
518 d
->ivec_irqs
= ivec_irqs
;
520 pci_create_simple(phb
->bus
, 0, "pbm-pci");
524 memset(is
, 0, sizeof(IOMMUState
));
526 /* APB secondary busses */
527 pci_dev
= pci_create_multifunction(phb
->bus
, PCI_DEVFN(1, 0), true,
529 br
= PCI_BRIDGE(pci_dev
);
530 pci_bridge_map_irq(br
, "Advanced PCI Bus secondary bridge 1",
532 qdev_init_nofail(&pci_dev
->qdev
);
533 *bus2
= pci_bridge_get_sec_bus(br
);
535 pci_dev
= pci_create_multifunction(phb
->bus
, PCI_DEVFN(1, 1), true,
537 br
= PCI_BRIDGE(pci_dev
);
538 pci_bridge_map_irq(br
, "Advanced PCI Bus secondary bridge 2",
540 qdev_init_nofail(&pci_dev
->qdev
);
541 *bus3
= pci_bridge_get_sec_bus(br
);
546 static void pci_pbm_reset(DeviceState
*d
)
549 APBState
*s
= APB_DEVICE(d
);
551 for (i
= 0; i
< 8; i
++) {
552 s
->pci_irq_map
[i
] &= PBM_PCI_IMR_MASK
;
554 for (i
= 0; i
< 32; i
++) {
555 s
->obio_irq_map
[i
] &= PBM_PCI_IMR_MASK
;
558 s
->irq_request
= NO_IRQ_REQUEST
;
559 s
->pci_irq_in
= 0ULL;
561 if (s
->nr_resets
++ == 0) {
563 s
->reset_control
= POR
;
567 static const MemoryRegionOps pci_config_ops
= {
568 .read
= apb_pci_config_read
,
569 .write
= apb_pci_config_write
,
570 .endianness
= DEVICE_NATIVE_ENDIAN
,
573 static int pci_pbm_init_device(SysBusDevice
*dev
)
579 for (i
= 0; i
< 8; i
++) {
580 s
->pci_irq_map
[i
] = (0x1f << 6) | (i
<< 2);
582 for (i
= 0; i
< 32; i
++) {
583 s
->obio_irq_map
[i
] = ((0x1f << 6) | 0x20) + i
;
585 s
->pbm_irqs
= qemu_allocate_irqs(pci_apb_set_irq
, s
, MAX_IVEC
);
586 s
->irq_request
= NO_IRQ_REQUEST
;
587 s
->pci_irq_in
= 0ULL;
590 memory_region_init_io(&s
->apb_config
, OBJECT(s
), &apb_config_ops
, s
,
591 "apb-config", 0x10000);
593 sysbus_init_mmio(dev
, &s
->apb_config
);
595 memory_region_init_io(&s
->pci_config
, OBJECT(s
), &pci_config_ops
, s
,
596 "apb-pci-config", 0x1000000);
598 sysbus_init_mmio(dev
, &s
->pci_config
);
601 memory_region_init_alias(&s
->pci_ioport
, OBJECT(s
), "apb-pci-ioport",
602 get_system_io(), 0, 0x10000);
604 sysbus_init_mmio(dev
, &s
->pci_ioport
);
609 static int pbm_pci_host_init(PCIDevice
*d
)
611 pci_set_word(d
->config
+ PCI_COMMAND
,
612 PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
613 pci_set_word(d
->config
+ PCI_STATUS
,
614 PCI_STATUS_FAST_BACK
| PCI_STATUS_66MHZ
|
615 PCI_STATUS_DEVSEL_MEDIUM
);
619 static void pbm_pci_host_class_init(ObjectClass
*klass
, void *data
)
621 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
622 DeviceClass
*dc
= DEVICE_CLASS(klass
);
624 k
->init
= pbm_pci_host_init
;
625 k
->vendor_id
= PCI_VENDOR_ID_SUN
;
626 k
->device_id
= PCI_DEVICE_ID_SUN_SABRE
;
627 k
->class_id
= PCI_CLASS_BRIDGE_HOST
;
629 * PCI-facing part of the host bridge, not usable without the
630 * host-facing part, which can't be device_add'ed, yet.
632 dc
->cannot_instantiate_with_device_add_yet
= true;
635 static const TypeInfo pbm_pci_host_info
= {
637 .parent
= TYPE_PCI_DEVICE
,
638 .instance_size
= sizeof(PCIDevice
),
639 .class_init
= pbm_pci_host_class_init
,
642 static void pbm_host_class_init(ObjectClass
*klass
, void *data
)
644 DeviceClass
*dc
= DEVICE_CLASS(klass
);
645 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
647 k
->init
= pci_pbm_init_device
;
648 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
649 dc
->reset
= pci_pbm_reset
;
652 static const TypeInfo pbm_host_info
= {
654 .parent
= TYPE_PCI_HOST_BRIDGE
,
655 .instance_size
= sizeof(APBState
),
656 .class_init
= pbm_host_class_init
,
659 static void pbm_pci_bridge_class_init(ObjectClass
*klass
, void *data
)
661 DeviceClass
*dc
= DEVICE_CLASS(klass
);
662 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
664 k
->init
= apb_pci_bridge_initfn
;
665 k
->exit
= pci_bridge_exitfn
;
666 k
->vendor_id
= PCI_VENDOR_ID_SUN
;
667 k
->device_id
= PCI_DEVICE_ID_SUN_SIMBA
;
669 k
->config_write
= pci_bridge_write_config
;
671 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
672 dc
->reset
= pci_bridge_reset
;
673 dc
->vmsd
= &vmstate_pci_device
;
676 static const TypeInfo pbm_pci_bridge_info
= {
677 .name
= "pbm-bridge",
678 .parent
= TYPE_PCI_BRIDGE
,
679 .class_init
= pbm_pci_bridge_class_init
,
682 static void pbm_register_types(void)
684 type_register_static(&pbm_host_info
);
685 type_register_static(&pbm_pci_host_info
);
686 type_register_static(&pbm_pci_bridge_info
);
689 type_init(pbm_register_types
)