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, ...)
51 * PBM: "UltraSPARC IIi User's Manual",
52 * http://www.sun.com/processors/manuals/805-0087.pdf
54 * APB: "Advanced PCI Bridge (APB) User's Manual",
55 * http://www.sun.com/processors/manuals/805-1251.pdf
58 #define PBM_PCI_IMR_MASK 0x7fffffff
59 #define PBM_PCI_IMR_ENABLED 0x80000000
62 #define SOFT_POR (1 << 30)
63 #define SOFT_XIR (1 << 29)
64 #define BTN_POR (1 << 28)
65 #define BTN_XIR (1 << 27)
66 #define RESET_MASK 0xf8000000
67 #define RESET_WCMASK 0x98000000
68 #define RESET_WMASK 0x60000000
71 #define NO_IRQ_REQUEST (MAX_IVEC + 1)
73 #define TYPE_APB "pbm"
75 #define APB_DEVICE(obj) \
76 OBJECT_CHECK(APBState, (obj), TYPE_APB)
78 typedef struct APBState
{
79 PCIHostState parent_obj
;
81 MemoryRegion apb_config
;
82 MemoryRegion pci_config
;
83 MemoryRegion pci_mmio
;
84 MemoryRegion pci_ioport
;
87 uint32_t pci_control
[16];
88 uint32_t pci_irq_map
[8];
89 uint32_t obio_irq_map
[32];
92 unsigned int irq_request
;
93 uint32_t reset_control
;
94 unsigned int nr_resets
;
97 static inline void pbm_set_request(APBState
*s
, unsigned int irq_num
)
99 APB_DPRINTF("%s: request irq %d\n", __func__
, irq_num
);
101 s
->irq_request
= irq_num
;
102 qemu_set_irq(s
->ivec_irqs
[irq_num
], 1);
105 static inline void pbm_check_irqs(APBState
*s
)
110 /* Previous request is not acknowledged, resubmit */
111 if (s
->irq_request
!= NO_IRQ_REQUEST
) {
112 pbm_set_request(s
, s
->irq_request
);
115 /* no request pending */
116 if (s
->pci_irq_in
== 0ULL) {
119 for (i
= 0; i
< 32; i
++) {
120 if (s
->pci_irq_in
& (1ULL << i
)) {
121 if (s
->pci_irq_map
[i
>> 2] & PBM_PCI_IMR_ENABLED
) {
122 pbm_set_request(s
, i
);
127 for (i
= 32; i
< 64; i
++) {
128 if (s
->pci_irq_in
& (1ULL << i
)) {
129 if (s
->obio_irq_map
[i
- 32] & PBM_PCI_IMR_ENABLED
) {
130 pbm_set_request(s
, i
);
137 static inline void pbm_clear_request(APBState
*s
, unsigned int irq_num
)
139 APB_DPRINTF("%s: clear request irq %d\n", __func__
, irq_num
);
140 qemu_set_irq(s
->ivec_irqs
[irq_num
], 0);
141 s
->irq_request
= NO_IRQ_REQUEST
;
144 static void apb_config_writel (void *opaque
, hwaddr addr
,
145 uint64_t val
, unsigned size
)
147 APBState
*s
= opaque
;
149 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" val %" PRIx64
"\n", __func__
, addr
, val
);
151 switch (addr
& 0xffff) {
152 case 0x30 ... 0x4f: /* DMA error registers */
153 /* XXX: not implemented yet */
155 case 0x200 ... 0x20b: /* IOMMU */
156 s
->iommu
[(addr
& 0xf) >> 2] = val
;
158 case 0x20c ... 0x3ff: /* IOMMU flush */
160 case 0xc00 ... 0xc3f: /* PCI interrupt control */
162 unsigned int ino
= (addr
& 0x3f) >> 3;
163 s
->pci_irq_map
[ino
] &= PBM_PCI_IMR_MASK
;
164 s
->pci_irq_map
[ino
] |= val
& ~PBM_PCI_IMR_MASK
;
165 if ((s
->irq_request
== ino
) && !(val
& ~PBM_PCI_IMR_MASK
)) {
166 pbm_clear_request(s
, ino
);
171 case 0x1000 ... 0x1080: /* OBIO interrupt control */
173 unsigned int ino
= ((addr
& 0xff) >> 3);
174 s
->obio_irq_map
[ino
] &= PBM_PCI_IMR_MASK
;
175 s
->obio_irq_map
[ino
] |= val
& ~PBM_PCI_IMR_MASK
;
176 if ((s
->irq_request
== (ino
| 0x20))
177 && !(val
& ~PBM_PCI_IMR_MASK
)) {
178 pbm_clear_request(s
, ino
| 0x20);
183 case 0x1400 ... 0x14ff: /* PCI interrupt clear */
185 unsigned int ino
= (addr
& 0xff) >> 5;
186 if ((s
->irq_request
/ 4) == ino
) {
187 pbm_clear_request(s
, s
->irq_request
);
192 case 0x1800 ... 0x1860: /* OBIO interrupt clear */
194 unsigned int ino
= ((addr
& 0xff) >> 3) | 0x20;
195 if (s
->irq_request
== ino
) {
196 pbm_clear_request(s
, ino
);
201 case 0x2000 ... 0x202f: /* PCI control */
202 s
->pci_control
[(addr
& 0x3f) >> 2] = val
;
204 case 0xf020 ... 0xf027: /* Reset control */
207 s
->reset_control
&= ~(val
& RESET_WCMASK
);
208 s
->reset_control
|= val
& RESET_WMASK
;
209 if (val
& SOFT_POR
) {
211 qemu_system_reset_request();
212 } else if (val
& SOFT_XIR
) {
213 qemu_system_reset_request();
217 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
218 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
219 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
220 case 0xf000 ... 0xf01f: /* FFB config, memory control */
227 static uint64_t apb_config_readl (void *opaque
,
228 hwaddr addr
, unsigned size
)
230 APBState
*s
= opaque
;
233 switch (addr
& 0xffff) {
234 case 0x30 ... 0x4f: /* DMA error registers */
236 /* XXX: not implemented yet */
238 case 0x200 ... 0x20b: /* IOMMU */
239 val
= s
->iommu
[(addr
& 0xf) >> 2];
241 case 0x20c ... 0x3ff: /* IOMMU flush */
244 case 0xc00 ... 0xc3f: /* PCI interrupt control */
246 val
= s
->pci_irq_map
[(addr
& 0x3f) >> 3];
251 case 0x1000 ... 0x1080: /* OBIO interrupt control */
253 val
= s
->obio_irq_map
[(addr
& 0xff) >> 3];
258 case 0x2000 ... 0x202f: /* PCI control */
259 val
= s
->pci_control
[(addr
& 0x3f) >> 2];
261 case 0xf020 ... 0xf027: /* Reset control */
263 val
= s
->reset_control
;
268 case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */
269 case 0xa400 ... 0xa67f: /* IOMMU diagnostics */
270 case 0xa800 ... 0xa80f: /* Interrupt diagnostics */
271 case 0xf000 ... 0xf01f: /* FFB config, memory control */
277 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" -> %x\n", __func__
, addr
, val
);
282 static const MemoryRegionOps apb_config_ops
= {
283 .read
= apb_config_readl
,
284 .write
= apb_config_writel
,
285 .endianness
= DEVICE_NATIVE_ENDIAN
,
288 static void apb_pci_config_write(void *opaque
, hwaddr addr
,
289 uint64_t val
, unsigned size
)
291 APBState
*s
= opaque
;
292 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
294 val
= qemu_bswap_len(val
, size
);
295 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" val %" PRIx64
"\n", __func__
, addr
, val
);
296 pci_data_write(phb
->bus
, addr
, val
, size
);
299 static uint64_t apb_pci_config_read(void *opaque
, hwaddr addr
,
303 APBState
*s
= opaque
;
304 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
306 ret
= pci_data_read(phb
->bus
, addr
, size
);
307 ret
= qemu_bswap_len(ret
, size
);
308 APB_DPRINTF("%s: addr " TARGET_FMT_plx
" -> %x\n", __func__
, addr
, ret
);
312 /* The APB host has an IRQ line for each IRQ line of each slot. */
313 static int pci_apb_map_irq(PCIDevice
*pci_dev
, int irq_num
)
315 return ((pci_dev
->devfn
& 0x18) >> 1) + irq_num
;
318 static int pci_pbm_map_irq(PCIDevice
*pci_dev
, int irq_num
)
321 if (pci_dev
->devfn
& 1)
325 return (bus_offset
+ (PCI_SLOT(pci_dev
->devfn
) << 2) + irq_num
) & 0x1f;
328 static void pci_apb_set_irq(void *opaque
, int irq_num
, int level
)
330 APBState
*s
= opaque
;
332 APB_DPRINTF("%s: set irq_in %d level %d\n", __func__
, irq_num
, level
);
333 /* PCI IRQ map onto the first 32 INO. */
336 s
->pci_irq_in
|= 1ULL << irq_num
;
337 if (s
->pci_irq_map
[irq_num
>> 2] & PBM_PCI_IMR_ENABLED
) {
338 pbm_set_request(s
, irq_num
);
341 s
->pci_irq_in
&= ~(1ULL << irq_num
);
344 /* OBIO IRQ map onto the next 32 INO. */
346 APB_DPRINTF("%s: set irq %d level %d\n", __func__
, irq_num
, level
);
347 s
->pci_irq_in
|= 1ULL << irq_num
;
348 if ((s
->irq_request
== NO_IRQ_REQUEST
)
349 && (s
->obio_irq_map
[irq_num
- 32] & PBM_PCI_IMR_ENABLED
)) {
350 pbm_set_request(s
, irq_num
);
353 s
->pci_irq_in
&= ~(1ULL << irq_num
);
358 static int apb_pci_bridge_initfn(PCIDevice
*dev
)
362 rc
= pci_bridge_initfn(dev
, TYPE_PCI_BUS
);
369 * According to PCI bridge spec, after reset
370 * bus master bit is off
371 * memory space enable bit is off
372 * According to manual (805-1251.pdf).
373 * the reset value should be zero unless the boot pin is tied high
374 * (which is true) and thus it should be PCI_COMMAND_MEMORY.
376 pci_set_word(dev
->config
+ PCI_COMMAND
,
378 pci_set_word(dev
->config
+ PCI_STATUS
,
379 PCI_STATUS_FAST_BACK
| PCI_STATUS_66MHZ
|
380 PCI_STATUS_DEVSEL_MEDIUM
);
384 PCIBus
*pci_apb_init(hwaddr special_base
,
386 qemu_irq
*ivec_irqs
, PCIBus
**bus2
, PCIBus
**bus3
,
396 /* Ultrasparc PBM main bus */
397 dev
= qdev_create(NULL
, TYPE_APB
);
398 qdev_init_nofail(dev
);
399 s
= SYS_BUS_DEVICE(dev
);
401 sysbus_mmio_map(s
, 0, special_base
);
402 /* PCI configuration space */
403 sysbus_mmio_map(s
, 1, special_base
+ 0x1000000ULL
);
405 sysbus_mmio_map(s
, 2, special_base
+ 0x2000000ULL
);
408 memory_region_init(&d
->pci_mmio
, OBJECT(s
), "pci-mmio", 0x100000000ULL
);
409 memory_region_add_subregion(get_system_memory(), mem_base
, &d
->pci_mmio
);
411 phb
= PCI_HOST_BRIDGE(dev
);
412 phb
->bus
= pci_register_bus(DEVICE(phb
), "pci",
413 pci_apb_set_irq
, pci_pbm_map_irq
, d
,
416 0, 32, TYPE_PCI_BUS
);
418 *pbm_irqs
= d
->pbm_irqs
;
419 d
->ivec_irqs
= ivec_irqs
;
421 pci_create_simple(phb
->bus
, 0, "pbm-pci");
423 /* APB secondary busses */
424 pci_dev
= pci_create_multifunction(phb
->bus
, PCI_DEVFN(1, 0), true,
426 br
= PCI_BRIDGE(pci_dev
);
427 pci_bridge_map_irq(br
, "Advanced PCI Bus secondary bridge 1",
429 qdev_init_nofail(&pci_dev
->qdev
);
430 *bus2
= pci_bridge_get_sec_bus(br
);
432 pci_dev
= pci_create_multifunction(phb
->bus
, PCI_DEVFN(1, 1), true,
434 br
= PCI_BRIDGE(pci_dev
);
435 pci_bridge_map_irq(br
, "Advanced PCI Bus secondary bridge 2",
437 qdev_init_nofail(&pci_dev
->qdev
);
438 *bus3
= pci_bridge_get_sec_bus(br
);
443 static void pci_pbm_reset(DeviceState
*d
)
446 APBState
*s
= APB_DEVICE(d
);
448 for (i
= 0; i
< 8; i
++) {
449 s
->pci_irq_map
[i
] &= PBM_PCI_IMR_MASK
;
451 for (i
= 0; i
< 32; i
++) {
452 s
->obio_irq_map
[i
] &= PBM_PCI_IMR_MASK
;
455 s
->irq_request
= NO_IRQ_REQUEST
;
456 s
->pci_irq_in
= 0ULL;
458 if (s
->nr_resets
++ == 0) {
460 s
->reset_control
= POR
;
464 static const MemoryRegionOps pci_config_ops
= {
465 .read
= apb_pci_config_read
,
466 .write
= apb_pci_config_write
,
467 .endianness
= DEVICE_NATIVE_ENDIAN
,
470 static int pci_pbm_init_device(SysBusDevice
*dev
)
476 for (i
= 0; i
< 8; i
++) {
477 s
->pci_irq_map
[i
] = (0x1f << 6) | (i
<< 2);
479 for (i
= 0; i
< 32; i
++) {
480 s
->obio_irq_map
[i
] = ((0x1f << 6) | 0x20) + i
;
482 s
->pbm_irqs
= qemu_allocate_irqs(pci_apb_set_irq
, s
, MAX_IVEC
);
483 s
->irq_request
= NO_IRQ_REQUEST
;
484 s
->pci_irq_in
= 0ULL;
487 memory_region_init_io(&s
->apb_config
, OBJECT(s
), &apb_config_ops
, s
,
488 "apb-config", 0x10000);
490 sysbus_init_mmio(dev
, &s
->apb_config
);
492 memory_region_init_io(&s
->pci_config
, OBJECT(s
), &pci_config_ops
, s
,
493 "apb-pci-config", 0x1000000);
495 sysbus_init_mmio(dev
, &s
->pci_config
);
498 memory_region_init_alias(&s
->pci_ioport
, OBJECT(s
), "apb-pci-ioport",
499 get_system_io(), 0, 0x10000);
501 sysbus_init_mmio(dev
, &s
->pci_ioport
);
506 static int pbm_pci_host_init(PCIDevice
*d
)
508 pci_set_word(d
->config
+ PCI_COMMAND
,
509 PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
510 pci_set_word(d
->config
+ PCI_STATUS
,
511 PCI_STATUS_FAST_BACK
| PCI_STATUS_66MHZ
|
512 PCI_STATUS_DEVSEL_MEDIUM
);
516 static void pbm_pci_host_class_init(ObjectClass
*klass
, void *data
)
518 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
520 k
->init
= pbm_pci_host_init
;
521 k
->vendor_id
= PCI_VENDOR_ID_SUN
;
522 k
->device_id
= PCI_DEVICE_ID_SUN_SABRE
;
523 k
->class_id
= PCI_CLASS_BRIDGE_HOST
;
526 static const TypeInfo pbm_pci_host_info
= {
528 .parent
= TYPE_PCI_DEVICE
,
529 .instance_size
= sizeof(PCIDevice
),
530 .class_init
= pbm_pci_host_class_init
,
533 static void pbm_host_class_init(ObjectClass
*klass
, void *data
)
535 DeviceClass
*dc
= DEVICE_CLASS(klass
);
536 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
538 k
->init
= pci_pbm_init_device
;
539 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
540 dc
->reset
= pci_pbm_reset
;
543 static const TypeInfo pbm_host_info
= {
545 .parent
= TYPE_PCI_HOST_BRIDGE
,
546 .instance_size
= sizeof(APBState
),
547 .class_init
= pbm_host_class_init
,
550 static void pbm_pci_bridge_class_init(ObjectClass
*klass
, void *data
)
552 DeviceClass
*dc
= DEVICE_CLASS(klass
);
553 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
555 k
->init
= apb_pci_bridge_initfn
;
556 k
->exit
= pci_bridge_exitfn
;
557 k
->vendor_id
= PCI_VENDOR_ID_SUN
;
558 k
->device_id
= PCI_DEVICE_ID_SUN_SIMBA
;
560 k
->config_write
= pci_bridge_write_config
;
562 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
563 dc
->reset
= pci_bridge_reset
;
564 dc
->vmsd
= &vmstate_pci_device
;
567 static const TypeInfo pbm_pci_bridge_info
= {
568 .name
= "pbm-bridge",
569 .parent
= TYPE_PCI_BRIDGE
,
570 .class_init
= pbm_pci_bridge_class_init
,
573 static void pbm_register_types(void)
575 type_register_static(&pbm_host_info
);
576 type_register_static(&pbm_pci_host_info
);
577 type_register_static(&pbm_pci_bridge_info
);
580 type_init(pbm_register_types
)