2 * HP-PARISC Dino PCI chipset emulation, as in B160L and similiar machines
4 * (C) 2017-2019 by Helge Deller <deller@gmx.de>
6 * This work is licensed under the GNU GPL license version 2 or later.
8 * Documentation available at:
9 * https://parisc.wiki.kernel.org/images-parisc/9/91/Dino_ers.pdf
10 * https://parisc.wiki.kernel.org/images-parisc/7/70/Dino_3_1_Errata.pdf
13 #include "qemu/osdep.h"
14 #include "qemu/module.h"
15 #include "qemu/units.h"
16 #include "qapi/error.h"
18 #include "hw/pci/pci_device.h"
19 #include "hw/pci/pci_bus.h"
20 #include "hw/qdev-properties.h"
21 #include "hw/pci-host/dino.h"
22 #include "migration/vmstate.h"
24 #include "qom/object.h"
28 * Dino can forward memory accesses from the CPU in the range between
29 * 0xf0800000 and 0xff000000 to the PCI bus.
31 static void gsc_to_pci_forwarding(DinoState
*s
)
33 uint32_t io_addr_en
, tmp
;
36 tmp
= extract32(s
->io_control
, 7, 2);
37 enabled
= (tmp
== 0x01);
38 io_addr_en
= s
->io_addr_en
;
39 /* Mask out first (=firmware) and last (=Dino) areas. */
40 io_addr_en
&= ~(BIT(31) | BIT(0));
42 memory_region_transaction_begin();
43 for (i
= 1; i
< 31; i
++) {
44 MemoryRegion
*mem
= &s
->pci_mem_alias
[i
];
45 if (enabled
&& (io_addr_en
& (1U << i
))) {
46 if (!memory_region_is_mapped(mem
)) {
47 uint32_t addr
= 0xf0000000 + i
* DINO_MEM_CHUNK_SIZE
;
48 memory_region_add_subregion(get_system_memory(), addr
, mem
);
50 } else if (memory_region_is_mapped(mem
)) {
51 memory_region_del_subregion(get_system_memory(), mem
);
54 memory_region_transaction_commit();
57 static bool dino_chip_mem_valid(void *opaque
, hwaddr addr
,
58 unsigned size
, bool is_write
,
75 case DINO_PCI_IO_DATA
:
77 case DINO_GMASK
... DINO_PCISTS
:
78 case DINO_MLTIM
... DINO_PCIWOR
:
82 case DINO_PCI_IO_DATA
+ 2:
85 case DINO_PCI_IO_DATA
+ 1:
86 case DINO_PCI_IO_DATA
+ 3:
89 trace_dino_chip_mem_valid(addr
, ret
);
93 static MemTxResult
dino_chip_read_with_attrs(void *opaque
, hwaddr addr
,
94 uint64_t *data
, unsigned size
,
97 DinoState
*s
= opaque
;
98 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
99 MemTxResult ret
= MEMTX_OK
;
105 case DINO_PCI_IO_DATA
... DINO_PCI_IO_DATA
+ 3:
106 /* Read from PCI IO space. */
107 io
= &address_space_io
;
108 ioaddr
= phb
->config_reg
+ (addr
& 3);
111 val
= address_space_ldub(io
, ioaddr
, attrs
, &ret
);
114 val
= address_space_lduw_be(io
, ioaddr
, attrs
, &ret
);
117 val
= address_space_ldl_be(io
, ioaddr
, attrs
, &ret
);
120 g_assert_not_reached();
127 case DINO_IO_ADDR_EN
:
130 case DINO_IO_CONTROL
:
148 /* Any read to IPR clears the register. */
155 val
= s
->ilr
& s
->imr
& ~s
->icr
;
158 val
= s
->ilr
& s
->imr
& s
->icr
;
163 case DINO_GMASK
... DINO_TLTIM
:
164 val
= s
->reg800
[(addr
- DINO_GMASK
) / 4];
165 if (addr
== DINO_PAMR
) {
166 val
&= ~0x01; /* LSB is hardwired to 0 */
168 if (addr
== DINO_MLTIM
) {
169 val
&= ~0x07; /* 3 LSB are hardwired to 0 */
171 if (addr
== DINO_BRDG_FEAT
) {
172 val
&= ~(0x10710E0ul
| 8); /* bits 5-7, 24 & 15 reserved */
177 /* Controlled by dino_chip_mem_valid above. */
178 g_assert_not_reached();
181 trace_dino_chip_read(addr
, val
);
186 static MemTxResult
dino_chip_write_with_attrs(void *opaque
, hwaddr addr
,
187 uint64_t val
, unsigned size
,
190 DinoState
*s
= opaque
;
191 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
197 trace_dino_chip_write(addr
, val
);
200 case DINO_IO_DATA
... DINO_PCI_IO_DATA
+ 3:
201 /* Write into PCI IO space. */
202 io
= &address_space_io
;
203 ioaddr
= phb
->config_reg
+ (addr
& 3);
206 address_space_stb(io
, ioaddr
, val
, attrs
, &ret
);
209 address_space_stw_be(io
, ioaddr
, val
, attrs
, &ret
);
212 address_space_stl_be(io
, ioaddr
, val
, attrs
, &ret
);
215 g_assert_not_reached();
220 s
->io_fbb_en
= val
& 0x03;
222 case DINO_IO_ADDR_EN
:
224 gsc_to_pci_forwarding(s
);
226 case DINO_IO_CONTROL
:
228 gsc_to_pci_forwarding(s
);
244 /* Any write to IPR clears the register. */
248 /* IO_COMMAND of CPU with client_id bits */
249 s
->toc_addr
= 0xFFFA0030 | (val
& 0x1e000);
255 /* These registers are read-only. */
258 case DINO_GMASK
... DINO_TLTIM
:
259 i
= (addr
- DINO_GMASK
) / 4;
260 val
&= reg800_keep_bits
[i
];
265 /* Controlled by dino_chip_mem_valid above. */
266 g_assert_not_reached();
271 static const MemoryRegionOps dino_chip_ops
= {
272 .read_with_attrs
= dino_chip_read_with_attrs
,
273 .write_with_attrs
= dino_chip_write_with_attrs
,
274 .endianness
= DEVICE_BIG_ENDIAN
,
276 .min_access_size
= 1,
277 .max_access_size
= 4,
278 .accepts
= dino_chip_mem_valid
,
281 .min_access_size
= 1,
282 .max_access_size
= 4,
286 static const VMStateDescription vmstate_dino
= {
289 .minimum_version_id
= 1,
290 .fields
= (VMStateField
[]) {
291 VMSTATE_UINT32(iar0
, DinoState
),
292 VMSTATE_UINT32(iar1
, DinoState
),
293 VMSTATE_UINT32(imr
, DinoState
),
294 VMSTATE_UINT32(ipr
, DinoState
),
295 VMSTATE_UINT32(icr
, DinoState
),
296 VMSTATE_UINT32(ilr
, DinoState
),
297 VMSTATE_UINT32(io_fbb_en
, DinoState
),
298 VMSTATE_UINT32(io_addr_en
, DinoState
),
299 VMSTATE_UINT32(io_control
, DinoState
),
300 VMSTATE_UINT32(toc_addr
, DinoState
),
301 VMSTATE_END_OF_LIST()
305 /* Unlike pci_config_data_le_ops, no check of high bit set in config_reg. */
307 static uint64_t dino_config_data_read(void *opaque
, hwaddr addr
, unsigned len
)
309 PCIHostState
*s
= opaque
;
310 return pci_data_read(s
->bus
, s
->config_reg
| (addr
& 3), len
);
313 static void dino_config_data_write(void *opaque
, hwaddr addr
,
314 uint64_t val
, unsigned len
)
316 PCIHostState
*s
= opaque
;
317 pci_data_write(s
->bus
, s
->config_reg
| (addr
& 3), val
, len
);
320 static const MemoryRegionOps dino_config_data_ops
= {
321 .read
= dino_config_data_read
,
322 .write
= dino_config_data_write
,
323 .endianness
= DEVICE_LITTLE_ENDIAN
,
326 static uint64_t dino_config_addr_read(void *opaque
, hwaddr addr
, unsigned len
)
328 DinoState
*s
= opaque
;
329 return s
->config_reg_dino
;
332 static void dino_config_addr_write(void *opaque
, hwaddr addr
,
333 uint64_t val
, unsigned len
)
335 PCIHostState
*s
= opaque
;
336 DinoState
*ds
= opaque
;
337 ds
->config_reg_dino
= val
; /* keep a copy of original value */
338 s
->config_reg
= val
& ~3U;
341 static const MemoryRegionOps dino_config_addr_ops
= {
342 .read
= dino_config_addr_read
,
343 .write
= dino_config_addr_write
,
344 .valid
.min_access_size
= 4,
345 .valid
.max_access_size
= 4,
346 .endianness
= DEVICE_BIG_ENDIAN
,
349 static AddressSpace
*dino_pcihost_set_iommu(PCIBus
*bus
, void *opaque
,
352 DinoState
*s
= opaque
;
358 * Dino interrupts are connected as shown on Page 78, Table 23
359 * (Little-endian bit numbers)
366 * 6 GSC External Interrupt
367 * 7 Bus Error for "less than fatal" mode
373 static void dino_set_irq(void *opaque
, int irq
, int level
)
375 DinoState
*s
= opaque
;
376 uint32_t bit
= 1u << irq
;
377 uint32_t old_ilr
= s
->ilr
;
380 uint32_t ena
= bit
& ~old_ilr
;
382 s
->ilr
= old_ilr
| bit
;
384 uint32_t iar
= (ena
& s
->icr
? s
->iar1
: s
->iar0
);
385 stl_be_phys(&address_space_memory
, iar
& -32, iar
& 31);
388 s
->ilr
= old_ilr
& ~bit
;
392 static int dino_pci_map_irq(PCIDevice
*d
, int irq_num
)
394 int slot
= PCI_SLOT(d
->devfn
);
396 assert(irq_num
>= 0 && irq_num
<= 3);
401 static void dino_pcihost_reset(DeviceState
*dev
)
403 DinoState
*s
= DINO_PCI_HOST_BRIDGE(dev
);
405 s
->iar0
= s
->iar1
= 0xFFFB0000 + 3; /* CPU_HPA + 3 */
406 s
->toc_addr
= 0xFFFA0030; /* IO_COMMAND of CPU */
409 static void dino_pcihost_realize(DeviceState
*dev
, Error
**errp
)
411 DinoState
*s
= DINO_PCI_HOST_BRIDGE(dev
);
413 /* Set up PCI view of memory: Bus master address space. */
414 memory_region_init(&s
->bm
, OBJECT(s
), "bm-dino", 4 * GiB
);
415 memory_region_init_alias(&s
->bm_ram_alias
, OBJECT(s
),
416 "bm-system", s
->memory_as
, 0,
417 0xf0000000 + DINO_MEM_CHUNK_SIZE
);
418 memory_region_init_alias(&s
->bm_pci_alias
, OBJECT(s
),
419 "bm-pci", &s
->pci_mem
,
420 0xf0000000 + DINO_MEM_CHUNK_SIZE
,
421 30 * DINO_MEM_CHUNK_SIZE
);
422 memory_region_init_alias(&s
->bm_cpu_alias
, OBJECT(s
),
423 "bm-cpu", s
->memory_as
, 0xfff00000,
425 memory_region_add_subregion(&s
->bm
, 0,
427 memory_region_add_subregion(&s
->bm
,
428 0xf0000000 + DINO_MEM_CHUNK_SIZE
,
430 memory_region_add_subregion(&s
->bm
, 0xfff00000,
433 address_space_init(&s
->bm_as
, &s
->bm
, "pci-bm");
436 static void dino_pcihost_unrealize(DeviceState
*dev
)
438 DinoState
*s
= DINO_PCI_HOST_BRIDGE(dev
);
440 address_space_destroy(&s
->bm_as
);
443 static void dino_pcihost_init(Object
*obj
)
445 DinoState
*s
= DINO_PCI_HOST_BRIDGE(obj
);
446 PCIHostState
*phb
= PCI_HOST_BRIDGE(obj
);
447 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
450 /* Dino PCI access from main memory. */
451 memory_region_init_io(&s
->this_mem
, OBJECT(s
), &dino_chip_ops
,
454 /* Dino PCI config. */
455 memory_region_init_io(&phb
->conf_mem
, OBJECT(phb
),
456 &dino_config_addr_ops
, DEVICE(s
),
458 memory_region_init_io(&phb
->data_mem
, OBJECT(phb
),
459 &dino_config_data_ops
, DEVICE(s
),
461 memory_region_add_subregion(&s
->this_mem
, DINO_PCI_CONFIG_ADDR
,
463 memory_region_add_subregion(&s
->this_mem
, DINO_CONFIG_DATA
,
466 /* Dino PCI bus memory. */
467 memory_region_init(&s
->pci_mem
, OBJECT(s
), "pci-memory", 4 * GiB
);
469 phb
->bus
= pci_register_root_bus(DEVICE(s
), "pci",
470 dino_set_irq
, dino_pci_map_irq
, s
,
471 &s
->pci_mem
, get_system_io(),
472 PCI_DEVFN(0, 0), 32, TYPE_PCI_BUS
);
474 /* Set up windows into PCI bus memory. */
475 for (i
= 1; i
< 31; i
++) {
476 uint32_t addr
= 0xf0000000 + i
* DINO_MEM_CHUNK_SIZE
;
477 char *name
= g_strdup_printf("PCI Outbound Window %d", i
);
478 memory_region_init_alias(&s
->pci_mem_alias
[i
], OBJECT(s
),
479 name
, &s
->pci_mem
, addr
,
480 DINO_MEM_CHUNK_SIZE
);
484 pci_setup_iommu(phb
->bus
, dino_pcihost_set_iommu
, s
);
486 sysbus_init_mmio(sbd
, &s
->this_mem
);
488 qdev_init_gpio_in(DEVICE(obj
), dino_set_irq
, DINO_IRQS
);
491 static Property dino_pcihost_properties
[] = {
492 DEFINE_PROP_LINK("memory-as", DinoState
, memory_as
, TYPE_MEMORY_REGION
,
494 DEFINE_PROP_END_OF_LIST(),
497 static void dino_pcihost_class_init(ObjectClass
*klass
, void *data
)
499 DeviceClass
*dc
= DEVICE_CLASS(klass
);
501 dc
->reset
= dino_pcihost_reset
;
502 dc
->realize
= dino_pcihost_realize
;
503 dc
->unrealize
= dino_pcihost_unrealize
;
504 device_class_set_props(dc
, dino_pcihost_properties
);
505 dc
->vmsd
= &vmstate_dino
;
508 static const TypeInfo dino_pcihost_info
= {
509 .name
= TYPE_DINO_PCI_HOST_BRIDGE
,
510 .parent
= TYPE_PCI_HOST_BRIDGE
,
511 .instance_init
= dino_pcihost_init
,
512 .instance_size
= sizeof(DinoState
),
513 .class_init
= dino_pcihost_class_init
,
516 static void dino_register_types(void)
518 type_register_static(&dino_pcihost_info
);
521 type_init(dino_register_types
)