2 * Emulation of the ibm,plb-pcix PCI controller
3 * This is found in some 440 SoCs e.g. the 460EX.
5 * Copyright (c) 2016-2018 BALATON Zoltan
7 * Derived from ppc4xx_pci.c and pci-host/ppce500.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, version 2, as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
25 #include "hw/ppc/ppc.h"
26 #include "hw/ppc/ppc4xx.h"
27 #include "hw/pci/pci.h"
28 #include "hw/pci/pci_host.h"
29 #include "exec/address-spaces.h"
45 #define TYPE_PPC440_PCIX_HOST_BRIDGE "ppc440-pcix-host"
46 #define PPC440_PCIX_HOST_BRIDGE(obj) \
47 OBJECT_CHECK(PPC440PCIXState, (obj), TYPE_PPC440_PCIX_HOST_BRIDGE)
49 #define PPC440_PCIX_NR_POMS 3
50 #define PPC440_PCIX_NR_PIMS 3
52 typedef struct PPC440PCIXState
{
53 PCIHostState parent_obj
;
56 struct PLBOutMap pom
[PPC440_PCIX_NR_POMS
];
57 struct PLBInMap pim
[PPC440_PCIX_NR_PIMS
];
59 qemu_irq irq
[PCI_NUM_PINS
];
63 MemoryRegion container
;
68 #define PPC440_REG_BASE 0x80000
69 #define PPC440_REG_SIZE 0xff
71 #define PCIC0_CFGADDR 0x0
72 #define PCIC0_CFGDATA 0x4
74 #define PCIX0_POM0LAL 0x68
75 #define PCIX0_POM0LAH 0x6c
76 #define PCIX0_POM0SA 0x70
77 #define PCIX0_POM0PCIAL 0x74
78 #define PCIX0_POM0PCIAH 0x78
79 #define PCIX0_POM1LAL 0x7c
80 #define PCIX0_POM1LAH 0x80
81 #define PCIX0_POM1SA 0x84
82 #define PCIX0_POM1PCIAL 0x88
83 #define PCIX0_POM1PCIAH 0x8c
84 #define PCIX0_POM2SA 0x90
86 #define PCIX0_PIM0SAL 0x98
87 #define PCIX0_PIM0LAL 0x9c
88 #define PCIX0_PIM0LAH 0xa0
89 #define PCIX0_PIM1SA 0xa4
90 #define PCIX0_PIM1LAL 0xa8
91 #define PCIX0_PIM1LAH 0xac
92 #define PCIX0_PIM2SAL 0xb0
93 #define PCIX0_PIM2LAL 0xb4
94 #define PCIX0_PIM2LAH 0xb8
95 #define PCIX0_PIM0SAH 0xf8
96 #define PCIX0_PIM2SAH 0xfc
98 #define PCIX0_STS 0xe0
100 #define PCI_ALL_SIZE (PPC440_REG_BASE + PPC440_REG_SIZE)
102 static void ppc440_pcix_clear_region(MemoryRegion
*parent
,
105 if (memory_region_is_mapped(mem
)) {
106 memory_region_del_subregion(parent
, mem
);
107 object_unparent(OBJECT(mem
));
112 static void ppc440_pcix_update_pim(PPC440PCIXState
*s
, int idx
)
114 MemoryRegion
*mem
= &s
->pim
[idx
].mr
;
118 /* Before we modify anything, unmap and destroy the region */
119 ppc440_pcix_clear_region(&s
->bm
, mem
);
121 if (!(s
->pim
[idx
].sa
& 1)) {
122 /* Not enabled, nothing to do */
126 name
= g_strdup_printf("PCI Inbound Window %d", idx
);
127 size
= ~(s
->pim
[idx
].sa
& ~7ULL) + 1;
128 memory_region_init_alias(mem
, OBJECT(s
), name
, get_system_memory(),
129 s
->pim
[idx
].la
, size
);
130 memory_region_add_subregion_overlap(&s
->bm
, 0, mem
, -1);
133 trace_ppc440_pcix_update_pim(idx
, size
, s
->pim
[idx
].la
);
137 static void ppc440_pcix_update_pom(PPC440PCIXState
*s
, int idx
)
139 MemoryRegion
*mem
= &s
->pom
[idx
].mr
;
140 MemoryRegion
*address_space_mem
= get_system_memory();
144 /* Before we modify anything, unmap and destroy the region */
145 ppc440_pcix_clear_region(address_space_mem
, mem
);
147 if (!(s
->pom
[idx
].sa
& 1)) {
148 /* Not enabled, nothing to do */
152 name
= g_strdup_printf("PCI Outbound Window %d", idx
);
153 size
= ~(s
->pom
[idx
].sa
& 0xfffffffe) + 1;
157 memory_region_init_alias(mem
, OBJECT(s
), name
, &s
->busmem
,
158 s
->pom
[idx
].pcia
, size
);
159 memory_region_add_subregion(address_space_mem
, s
->pom
[idx
].la
, mem
);
162 trace_ppc440_pcix_update_pom(idx
, size
, s
->pom
[idx
].la
, s
->pom
[idx
].pcia
);
165 static void ppc440_pcix_reg_write4(void *opaque
, hwaddr addr
,
166 uint64_t val
, unsigned size
)
168 struct PPC440PCIXState
*s
= opaque
;
170 trace_ppc440_pcix_reg_read(addr
, val
);
172 case PCI_VENDOR_ID
... PCI_MAX_LAT
:
173 stl_le_p(s
->dev
->config
+ addr
, val
);
177 s
->pom
[0].la
&= 0xffffffff00000000ULL
;
179 ppc440_pcix_update_pom(s
, 0);
182 s
->pom
[0].la
&= 0xffffffffULL
;
183 s
->pom
[0].la
|= val
<< 32;
184 ppc440_pcix_update_pom(s
, 0);
188 ppc440_pcix_update_pom(s
, 0);
190 case PCIX0_POM0PCIAL
:
191 s
->pom
[0].pcia
&= 0xffffffff00000000ULL
;
192 s
->pom
[0].pcia
|= val
;
193 ppc440_pcix_update_pom(s
, 0);
195 case PCIX0_POM0PCIAH
:
196 s
->pom
[0].pcia
&= 0xffffffffULL
;
197 s
->pom
[0].pcia
|= val
<< 32;
198 ppc440_pcix_update_pom(s
, 0);
201 s
->pom
[1].la
&= 0xffffffff00000000ULL
;
203 ppc440_pcix_update_pom(s
, 1);
206 s
->pom
[1].la
&= 0xffffffffULL
;
207 s
->pom
[1].la
|= val
<< 32;
208 ppc440_pcix_update_pom(s
, 1);
212 ppc440_pcix_update_pom(s
, 1);
214 case PCIX0_POM1PCIAL
:
215 s
->pom
[1].pcia
&= 0xffffffff00000000ULL
;
216 s
->pom
[1].pcia
|= val
;
217 ppc440_pcix_update_pom(s
, 1);
219 case PCIX0_POM1PCIAH
:
220 s
->pom
[1].pcia
&= 0xffffffffULL
;
221 s
->pom
[1].pcia
|= val
<< 32;
222 ppc440_pcix_update_pom(s
, 1);
229 s
->pim
[0].sa
&= 0xffffffff00000000ULL
;
231 ppc440_pcix_update_pim(s
, 0);
234 s
->pim
[0].la
&= 0xffffffff00000000ULL
;
236 ppc440_pcix_update_pim(s
, 0);
239 s
->pim
[0].la
&= 0xffffffffULL
;
240 s
->pim
[0].la
|= val
<< 32;
241 ppc440_pcix_update_pim(s
, 0);
245 ppc440_pcix_update_pim(s
, 1);
248 s
->pim
[1].la
&= 0xffffffff00000000ULL
;
250 ppc440_pcix_update_pim(s
, 1);
253 s
->pim
[1].la
&= 0xffffffffULL
;
254 s
->pim
[1].la
|= val
<< 32;
255 ppc440_pcix_update_pim(s
, 1);
258 s
->pim
[2].sa
&= 0xffffffff00000000ULL
;
260 ppc440_pcix_update_pim(s
, 2);
263 s
->pim
[2].la
&= 0xffffffff00000000ULL
;
265 ppc440_pcix_update_pim(s
, 2);
268 s
->pim
[2].la
&= 0xffffffffULL
;
269 s
->pim
[2].la
|= val
<< 32;
270 ppc440_pcix_update_pim(s
, 2);
278 s
->pim
[0].sa
&= 0xffffffffULL
;
279 s
->pim
[0].sa
|= val
<< 32;
280 ppc440_pcix_update_pim(s
, 0);
283 s
->pim
[2].sa
&= 0xffffffffULL
;
284 s
->pim
[2].sa
|= val
<< 32;
285 ppc440_pcix_update_pim(s
, 2);
289 error_report("%s: unhandled PCI internal register 0x%lx", __func__
,
290 (unsigned long)addr
);
295 static uint64_t ppc440_pcix_reg_read4(void *opaque
, hwaddr addr
,
298 struct PPC440PCIXState
*s
= opaque
;
302 case PCI_VENDOR_ID
... PCI_MAX_LAT
:
303 val
= ldl_le_p(s
->dev
->config
+ addr
);
310 val
= s
->pom
[0].la
>> 32;
315 case PCIX0_POM0PCIAL
:
316 val
= s
->pom
[0].pcia
;
318 case PCIX0_POM0PCIAH
:
319 val
= s
->pom
[0].pcia
>> 32;
325 val
= s
->pom
[1].la
>> 32;
330 case PCIX0_POM1PCIAL
:
331 val
= s
->pom
[1].pcia
;
333 case PCIX0_POM1PCIAH
:
334 val
= s
->pom
[1].pcia
>> 32;
347 val
= s
->pim
[0].la
>> 32;
356 val
= s
->pim
[1].la
>> 32;
365 val
= s
->pim
[2].la
>> 32;
373 val
= s
->pim
[0].sa
>> 32;
376 val
= s
->pim
[2].sa
>> 32;
380 error_report("%s: invalid PCI internal register 0x%lx", __func__
,
381 (unsigned long)addr
);
385 trace_ppc440_pcix_reg_read(addr
, val
);
389 static const MemoryRegionOps pci_reg_ops
= {
390 .read
= ppc440_pcix_reg_read4
,
391 .write
= ppc440_pcix_reg_write4
,
392 .endianness
= DEVICE_LITTLE_ENDIAN
,
395 static void ppc440_pcix_reset(DeviceState
*dev
)
397 struct PPC440PCIXState
*s
= PPC440_PCIX_HOST_BRIDGE(dev
);
400 for (i
= 0; i
< PPC440_PCIX_NR_POMS
; i
++) {
401 ppc440_pcix_clear_region(get_system_memory(), &s
->pom
[i
].mr
);
403 for (i
= 0; i
< PPC440_PCIX_NR_PIMS
; i
++) {
404 ppc440_pcix_clear_region(&s
->bm
, &s
->pim
[i
].mr
);
406 memset(s
->pom
, 0, sizeof(s
->pom
));
407 memset(s
->pim
, 0, sizeof(s
->pim
));
408 for (i
= 0; i
< PPC440_PCIX_NR_PIMS
; i
++) {
409 s
->pim
[i
].sa
= 0xffffffff00000000ULL
;
414 /* All pins from each slot are tied to a single board IRQ.
415 * This may need further refactoring for other boards. */
416 static int ppc440_pcix_map_irq(PCIDevice
*pci_dev
, int irq_num
)
418 int slot
= pci_dev
->devfn
>> 3;
419 trace_ppc440_pcix_map_irq(pci_dev
->devfn
, irq_num
, slot
);
423 static void ppc440_pcix_set_irq(void *opaque
, int irq_num
, int level
)
425 qemu_irq
*pci_irqs
= opaque
;
427 trace_ppc440_pcix_set_irq(irq_num
);
429 error_report("%s: PCI irq %d", __func__
, irq_num
);
432 qemu_set_irq(pci_irqs
[irq_num
], level
);
435 static AddressSpace
*ppc440_pcix_set_iommu(PCIBus
*b
, void *opaque
, int devfn
)
437 PPC440PCIXState
*s
= opaque
;
442 /* The default pci_host_data_{read,write} functions in pci/pci_host.c
443 * deny access to registers without bit 31 set but our clients want
444 * this to work so we have to override these here */
445 static void pci_host_data_write(void *opaque
, hwaddr addr
,
446 uint64_t val
, unsigned len
)
448 PCIHostState
*s
= opaque
;
449 pci_data_write(s
->bus
, s
->config_reg
| (addr
& 3), val
, len
);
452 static uint64_t pci_host_data_read(void *opaque
,
453 hwaddr addr
, unsigned len
)
455 PCIHostState
*s
= opaque
;
457 val
= pci_data_read(s
->bus
, s
->config_reg
| (addr
& 3), len
);
461 const MemoryRegionOps ppc440_pcix_host_data_ops
= {
462 .read
= pci_host_data_read
,
463 .write
= pci_host_data_write
,
464 .endianness
= DEVICE_LITTLE_ENDIAN
,
467 static int ppc440_pcix_initfn(SysBusDevice
*dev
)
473 h
= PCI_HOST_BRIDGE(dev
);
474 s
= PPC440_PCIX_HOST_BRIDGE(dev
);
476 for (i
= 0; i
< ARRAY_SIZE(s
->irq
); i
++) {
477 sysbus_init_irq(dev
, &s
->irq
[i
]);
480 memory_region_init(&s
->busmem
, OBJECT(dev
), "pci bus memory", UINT64_MAX
);
481 h
->bus
= pci_register_root_bus(DEVICE(dev
), NULL
, ppc440_pcix_set_irq
,
482 ppc440_pcix_map_irq
, s
->irq
, &s
->busmem
,
483 get_system_io(), PCI_DEVFN(0, 0), 4, TYPE_PCI_BUS
);
485 s
->dev
= pci_create_simple(h
->bus
, PCI_DEVFN(0, 0), "ppc4xx-host-bridge");
487 memory_region_init(&s
->bm
, OBJECT(s
), "bm-ppc440-pcix", UINT64_MAX
);
488 memory_region_add_subregion(&s
->bm
, 0x0, &s
->busmem
);
489 address_space_init(&s
->bm_as
, &s
->bm
, "pci-bm");
490 pci_setup_iommu(h
->bus
, ppc440_pcix_set_iommu
, s
);
492 memory_region_init(&s
->container
, OBJECT(s
), "pci-container", PCI_ALL_SIZE
);
493 memory_region_init_io(&h
->conf_mem
, OBJECT(s
), &pci_host_conf_le_ops
,
494 h
, "pci-conf-idx", 4);
495 memory_region_init_io(&h
->data_mem
, OBJECT(s
), &ppc440_pcix_host_data_ops
,
496 h
, "pci-conf-data", 4);
497 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pci_reg_ops
, s
,
498 "pci.reg", PPC440_REG_SIZE
);
499 memory_region_add_subregion(&s
->container
, PCIC0_CFGADDR
, &h
->conf_mem
);
500 memory_region_add_subregion(&s
->container
, PCIC0_CFGDATA
, &h
->data_mem
);
501 memory_region_add_subregion(&s
->container
, PPC440_REG_BASE
, &s
->iomem
);
502 sysbus_init_mmio(dev
, &s
->container
);
507 static void ppc440_pcix_class_init(ObjectClass
*klass
, void *data
)
509 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
510 DeviceClass
*dc
= DEVICE_CLASS(klass
);
512 k
->init
= ppc440_pcix_initfn
;
513 dc
->reset
= ppc440_pcix_reset
;
516 static const TypeInfo ppc440_pcix_info
= {
517 .name
= TYPE_PPC440_PCIX_HOST_BRIDGE
,
518 .parent
= TYPE_PCI_HOST_BRIDGE
,
519 .instance_size
= sizeof(PPC440PCIXState
),
520 .class_init
= ppc440_pcix_class_init
,
523 static void ppc440_pcix_register_types(void)
525 type_register_static(&ppc440_pcix_info
);
528 type_init(ppc440_pcix_register_types
)