xref: /openbmc/qemu/hw/pci-host/dino.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
10db9350eSMark Cave-Ayland /*
2f1c0cff8SMichael Tokarev  * HP-PARISC Dino PCI chipset emulation, as in B160L and similar machines
30db9350eSMark Cave-Ayland  *
40db9350eSMark Cave-Ayland  * (C) 2017-2019 by Helge Deller <deller@gmx.de>
50db9350eSMark Cave-Ayland  *
60db9350eSMark Cave-Ayland  * This work is licensed under the GNU GPL license version 2 or later.
70db9350eSMark Cave-Ayland  *
80db9350eSMark Cave-Ayland  * Documentation available at:
90db9350eSMark Cave-Ayland  * https://parisc.wiki.kernel.org/images-parisc/9/91/Dino_ers.pdf
100db9350eSMark Cave-Ayland  * https://parisc.wiki.kernel.org/images-parisc/7/70/Dino_3_1_Errata.pdf
110db9350eSMark Cave-Ayland  */
120db9350eSMark Cave-Ayland 
130db9350eSMark Cave-Ayland #include "qemu/osdep.h"
140db9350eSMark Cave-Ayland #include "qemu/module.h"
150db9350eSMark Cave-Ayland #include "qemu/units.h"
160db9350eSMark Cave-Ayland #include "qapi/error.h"
170db9350eSMark Cave-Ayland #include "hw/irq.h"
18edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
190db9350eSMark Cave-Ayland #include "hw/pci/pci_bus.h"
200db9350eSMark Cave-Ayland #include "hw/qdev-properties.h"
210db9350eSMark Cave-Ayland #include "hw/pci-host/dino.h"
220db9350eSMark Cave-Ayland #include "migration/vmstate.h"
230db9350eSMark Cave-Ayland #include "trace.h"
240db9350eSMark Cave-Ayland #include "qom/object.h"
250db9350eSMark Cave-Ayland 
260db9350eSMark Cave-Ayland 
270db9350eSMark Cave-Ayland /*
280db9350eSMark Cave-Ayland  * Dino can forward memory accesses from the CPU in the range between
290db9350eSMark Cave-Ayland  * 0xf0800000 and 0xff000000 to the PCI bus.
300db9350eSMark Cave-Ayland  */
gsc_to_pci_forwarding(DinoState * s)310db9350eSMark Cave-Ayland static void gsc_to_pci_forwarding(DinoState *s)
320db9350eSMark Cave-Ayland {
330db9350eSMark Cave-Ayland     uint32_t io_addr_en, tmp;
340db9350eSMark Cave-Ayland     int enabled, i;
350db9350eSMark Cave-Ayland 
360db9350eSMark Cave-Ayland     tmp = extract32(s->io_control, 7, 2);
370db9350eSMark Cave-Ayland     enabled = (tmp == 0x01);
380db9350eSMark Cave-Ayland     io_addr_en = s->io_addr_en;
390db9350eSMark Cave-Ayland     /* Mask out first (=firmware) and last (=Dino) areas. */
400db9350eSMark Cave-Ayland     io_addr_en &= ~(BIT(31) | BIT(0));
410db9350eSMark Cave-Ayland 
420db9350eSMark Cave-Ayland     memory_region_transaction_begin();
430db9350eSMark Cave-Ayland     for (i = 1; i < 31; i++) {
440db9350eSMark Cave-Ayland         MemoryRegion *mem = &s->pci_mem_alias[i];
450db9350eSMark Cave-Ayland         if (enabled && (io_addr_en & (1U << i))) {
460db9350eSMark Cave-Ayland             if (!memory_region_is_mapped(mem)) {
470db9350eSMark Cave-Ayland                 uint32_t addr = 0xf0000000 + i * DINO_MEM_CHUNK_SIZE;
480db9350eSMark Cave-Ayland                 memory_region_add_subregion(get_system_memory(), addr, mem);
490db9350eSMark Cave-Ayland             }
500db9350eSMark Cave-Ayland         } else if (memory_region_is_mapped(mem)) {
510db9350eSMark Cave-Ayland             memory_region_del_subregion(get_system_memory(), mem);
520db9350eSMark Cave-Ayland         }
530db9350eSMark Cave-Ayland     }
540db9350eSMark Cave-Ayland     memory_region_transaction_commit();
550db9350eSMark Cave-Ayland }
560db9350eSMark Cave-Ayland 
dino_chip_mem_valid(void * opaque,hwaddr addr,unsigned size,bool is_write,MemTxAttrs attrs)570db9350eSMark Cave-Ayland static bool dino_chip_mem_valid(void *opaque, hwaddr addr,
580db9350eSMark Cave-Ayland                                 unsigned size, bool is_write,
590db9350eSMark Cave-Ayland                                 MemTxAttrs attrs)
600db9350eSMark Cave-Ayland {
610db9350eSMark Cave-Ayland     bool ret = false;
620db9350eSMark Cave-Ayland 
630db9350eSMark Cave-Ayland     switch (addr) {
640db9350eSMark Cave-Ayland     case DINO_IAR0:
650db9350eSMark Cave-Ayland     case DINO_IAR1:
660db9350eSMark Cave-Ayland     case DINO_IRR0:
670db9350eSMark Cave-Ayland     case DINO_IRR1:
680db9350eSMark Cave-Ayland     case DINO_IMR:
690db9350eSMark Cave-Ayland     case DINO_IPR:
700db9350eSMark Cave-Ayland     case DINO_ICR:
710db9350eSMark Cave-Ayland     case DINO_ILR:
720db9350eSMark Cave-Ayland     case DINO_IO_CONTROL:
730db9350eSMark Cave-Ayland     case DINO_IO_FBB_EN:
740db9350eSMark Cave-Ayland     case DINO_IO_ADDR_EN:
750db9350eSMark Cave-Ayland     case DINO_PCI_IO_DATA:
760db9350eSMark Cave-Ayland     case DINO_TOC_ADDR:
770db9350eSMark Cave-Ayland     case DINO_GMASK ... DINO_PCISTS:
780db9350eSMark Cave-Ayland     case DINO_MLTIM ... DINO_PCIWOR:
790db9350eSMark Cave-Ayland     case DINO_TLTIM:
800db9350eSMark Cave-Ayland         ret = true;
810db9350eSMark Cave-Ayland         break;
820db9350eSMark Cave-Ayland     case DINO_PCI_IO_DATA + 2:
830db9350eSMark Cave-Ayland         ret = (size <= 2);
840db9350eSMark Cave-Ayland         break;
850db9350eSMark Cave-Ayland     case DINO_PCI_IO_DATA + 1:
860db9350eSMark Cave-Ayland     case DINO_PCI_IO_DATA + 3:
870db9350eSMark Cave-Ayland         ret = (size == 1);
880db9350eSMark Cave-Ayland     }
890db9350eSMark Cave-Ayland     trace_dino_chip_mem_valid(addr, ret);
900db9350eSMark Cave-Ayland     return ret;
910db9350eSMark Cave-Ayland }
920db9350eSMark Cave-Ayland 
dino_chip_read_with_attrs(void * opaque,hwaddr addr,uint64_t * data,unsigned size,MemTxAttrs attrs)930db9350eSMark Cave-Ayland static MemTxResult dino_chip_read_with_attrs(void *opaque, hwaddr addr,
940db9350eSMark Cave-Ayland                                              uint64_t *data, unsigned size,
950db9350eSMark Cave-Ayland                                              MemTxAttrs attrs)
960db9350eSMark Cave-Ayland {
970db9350eSMark Cave-Ayland     DinoState *s = opaque;
980db9350eSMark Cave-Ayland     PCIHostState *phb = PCI_HOST_BRIDGE(s);
990db9350eSMark Cave-Ayland     MemTxResult ret = MEMTX_OK;
1000db9350eSMark Cave-Ayland     AddressSpace *io;
1010db9350eSMark Cave-Ayland     uint16_t ioaddr;
1020db9350eSMark Cave-Ayland     uint32_t val;
1030db9350eSMark Cave-Ayland 
1040db9350eSMark Cave-Ayland     switch (addr) {
1050db9350eSMark Cave-Ayland     case DINO_PCI_IO_DATA ... DINO_PCI_IO_DATA + 3:
1060db9350eSMark Cave-Ayland         /* Read from PCI IO space. */
1070db9350eSMark Cave-Ayland         io = &address_space_io;
1080db9350eSMark Cave-Ayland         ioaddr = phb->config_reg + (addr & 3);
1090db9350eSMark Cave-Ayland         switch (size) {
1100db9350eSMark Cave-Ayland         case 1:
1110db9350eSMark Cave-Ayland             val = address_space_ldub(io, ioaddr, attrs, &ret);
1120db9350eSMark Cave-Ayland             break;
1130db9350eSMark Cave-Ayland         case 2:
1140db9350eSMark Cave-Ayland             val = address_space_lduw_be(io, ioaddr, attrs, &ret);
1150db9350eSMark Cave-Ayland             break;
1160db9350eSMark Cave-Ayland         case 4:
1170db9350eSMark Cave-Ayland             val = address_space_ldl_be(io, ioaddr, attrs, &ret);
1180db9350eSMark Cave-Ayland             break;
1190db9350eSMark Cave-Ayland         default:
1200db9350eSMark Cave-Ayland             g_assert_not_reached();
1210db9350eSMark Cave-Ayland         }
1220db9350eSMark Cave-Ayland         break;
1230db9350eSMark Cave-Ayland 
1240db9350eSMark Cave-Ayland     case DINO_IO_FBB_EN:
1250db9350eSMark Cave-Ayland         val = s->io_fbb_en;
1260db9350eSMark Cave-Ayland         break;
1270db9350eSMark Cave-Ayland     case DINO_IO_ADDR_EN:
1280db9350eSMark Cave-Ayland         val = s->io_addr_en;
1290db9350eSMark Cave-Ayland         break;
1300db9350eSMark Cave-Ayland     case DINO_IO_CONTROL:
1310db9350eSMark Cave-Ayland         val = s->io_control;
1320db9350eSMark Cave-Ayland         break;
1330db9350eSMark Cave-Ayland 
1340db9350eSMark Cave-Ayland     case DINO_IAR0:
1350db9350eSMark Cave-Ayland         val = s->iar0;
1360db9350eSMark Cave-Ayland         break;
1370db9350eSMark Cave-Ayland     case DINO_IAR1:
1380db9350eSMark Cave-Ayland         val = s->iar1;
1390db9350eSMark Cave-Ayland         break;
1400db9350eSMark Cave-Ayland     case DINO_IMR:
1410db9350eSMark Cave-Ayland         val = s->imr;
1420db9350eSMark Cave-Ayland         break;
1430db9350eSMark Cave-Ayland     case DINO_ICR:
1440db9350eSMark Cave-Ayland         val = s->icr;
1450db9350eSMark Cave-Ayland         break;
1460db9350eSMark Cave-Ayland     case DINO_IPR:
1470db9350eSMark Cave-Ayland         val = s->ipr;
1480db9350eSMark Cave-Ayland         /* Any read to IPR clears the register.  */
1490db9350eSMark Cave-Ayland         s->ipr = 0;
1500db9350eSMark Cave-Ayland         break;
1510db9350eSMark Cave-Ayland     case DINO_ILR:
1520db9350eSMark Cave-Ayland         val = s->ilr;
1530db9350eSMark Cave-Ayland         break;
1540db9350eSMark Cave-Ayland     case DINO_IRR0:
1550db9350eSMark Cave-Ayland         val = s->ilr & s->imr & ~s->icr;
1560db9350eSMark Cave-Ayland         break;
1570db9350eSMark Cave-Ayland     case DINO_IRR1:
1580db9350eSMark Cave-Ayland         val = s->ilr & s->imr & s->icr;
1590db9350eSMark Cave-Ayland         break;
1600db9350eSMark Cave-Ayland     case DINO_TOC_ADDR:
1610db9350eSMark Cave-Ayland         val = s->toc_addr;
1620db9350eSMark Cave-Ayland         break;
1630db9350eSMark Cave-Ayland     case DINO_GMASK ... DINO_TLTIM:
1640db9350eSMark Cave-Ayland         val = s->reg800[(addr - DINO_GMASK) / 4];
1650db9350eSMark Cave-Ayland         if (addr == DINO_PAMR) {
1660db9350eSMark Cave-Ayland             val &= ~0x01;  /* LSB is hardwired to 0 */
1670db9350eSMark Cave-Ayland         }
1680db9350eSMark Cave-Ayland         if (addr == DINO_MLTIM) {
1690db9350eSMark Cave-Ayland             val &= ~0x07;  /* 3 LSB are hardwired to 0 */
1700db9350eSMark Cave-Ayland         }
1710db9350eSMark Cave-Ayland         if (addr == DINO_BRDG_FEAT) {
1720db9350eSMark Cave-Ayland             val &= ~(0x10710E0ul | 8); /* bits 5-7, 24 & 15 reserved */
1730db9350eSMark Cave-Ayland         }
1740db9350eSMark Cave-Ayland         break;
1750db9350eSMark Cave-Ayland 
1760db9350eSMark Cave-Ayland     default:
1770db9350eSMark Cave-Ayland         /* Controlled by dino_chip_mem_valid above.  */
1780db9350eSMark Cave-Ayland         g_assert_not_reached();
1790db9350eSMark Cave-Ayland     }
1800db9350eSMark Cave-Ayland 
1810db9350eSMark Cave-Ayland     trace_dino_chip_read(addr, val);
1820db9350eSMark Cave-Ayland     *data = val;
1830db9350eSMark Cave-Ayland     return ret;
1840db9350eSMark Cave-Ayland }
1850db9350eSMark Cave-Ayland 
dino_chip_write_with_attrs(void * opaque,hwaddr addr,uint64_t val,unsigned size,MemTxAttrs attrs)1860db9350eSMark Cave-Ayland static MemTxResult dino_chip_write_with_attrs(void *opaque, hwaddr addr,
1870db9350eSMark Cave-Ayland                                               uint64_t val, unsigned size,
1880db9350eSMark Cave-Ayland                                               MemTxAttrs attrs)
1890db9350eSMark Cave-Ayland {
1900db9350eSMark Cave-Ayland     DinoState *s = opaque;
1910db9350eSMark Cave-Ayland     PCIHostState *phb = PCI_HOST_BRIDGE(s);
1920db9350eSMark Cave-Ayland     AddressSpace *io;
1930db9350eSMark Cave-Ayland     MemTxResult ret;
1940db9350eSMark Cave-Ayland     uint16_t ioaddr;
1950db9350eSMark Cave-Ayland     int i;
1960db9350eSMark Cave-Ayland 
1970db9350eSMark Cave-Ayland     trace_dino_chip_write(addr, val);
1980db9350eSMark Cave-Ayland 
1990db9350eSMark Cave-Ayland     switch (addr) {
2000db9350eSMark Cave-Ayland     case DINO_IO_DATA ... DINO_PCI_IO_DATA + 3:
2010db9350eSMark Cave-Ayland         /* Write into PCI IO space.  */
2020db9350eSMark Cave-Ayland         io = &address_space_io;
2030db9350eSMark Cave-Ayland         ioaddr = phb->config_reg + (addr & 3);
2040db9350eSMark Cave-Ayland         switch (size) {
2050db9350eSMark Cave-Ayland         case 1:
2060db9350eSMark Cave-Ayland             address_space_stb(io, ioaddr, val, attrs, &ret);
2070db9350eSMark Cave-Ayland             break;
2080db9350eSMark Cave-Ayland         case 2:
2090db9350eSMark Cave-Ayland             address_space_stw_be(io, ioaddr, val, attrs, &ret);
2100db9350eSMark Cave-Ayland             break;
2110db9350eSMark Cave-Ayland         case 4:
2120db9350eSMark Cave-Ayland             address_space_stl_be(io, ioaddr, val, attrs, &ret);
2130db9350eSMark Cave-Ayland             break;
2140db9350eSMark Cave-Ayland         default:
2150db9350eSMark Cave-Ayland             g_assert_not_reached();
2160db9350eSMark Cave-Ayland         }
2170db9350eSMark Cave-Ayland         return ret;
2180db9350eSMark Cave-Ayland 
2190db9350eSMark Cave-Ayland     case DINO_IO_FBB_EN:
2200db9350eSMark Cave-Ayland         s->io_fbb_en = val & 0x03;
2210db9350eSMark Cave-Ayland         break;
2220db9350eSMark Cave-Ayland     case DINO_IO_ADDR_EN:
2230db9350eSMark Cave-Ayland         s->io_addr_en = val;
2240db9350eSMark Cave-Ayland         gsc_to_pci_forwarding(s);
2250db9350eSMark Cave-Ayland         break;
2260db9350eSMark Cave-Ayland     case DINO_IO_CONTROL:
2270db9350eSMark Cave-Ayland         s->io_control = val;
2280db9350eSMark Cave-Ayland         gsc_to_pci_forwarding(s);
2290db9350eSMark Cave-Ayland         break;
2300db9350eSMark Cave-Ayland 
2310db9350eSMark Cave-Ayland     case DINO_IAR0:
2320db9350eSMark Cave-Ayland         s->iar0 = val;
2330db9350eSMark Cave-Ayland         break;
2340db9350eSMark Cave-Ayland     case DINO_IAR1:
2350db9350eSMark Cave-Ayland         s->iar1 = val;
2360db9350eSMark Cave-Ayland         break;
2370db9350eSMark Cave-Ayland     case DINO_IMR:
2380db9350eSMark Cave-Ayland         s->imr = val;
2390db9350eSMark Cave-Ayland         break;
2400db9350eSMark Cave-Ayland     case DINO_ICR:
2410db9350eSMark Cave-Ayland         s->icr = val;
2420db9350eSMark Cave-Ayland         break;
2430db9350eSMark Cave-Ayland     case DINO_IPR:
2440db9350eSMark Cave-Ayland         /* Any write to IPR clears the register.  */
2450db9350eSMark Cave-Ayland         s->ipr = 0;
2460db9350eSMark Cave-Ayland         break;
2470db9350eSMark Cave-Ayland     case DINO_TOC_ADDR:
2480db9350eSMark Cave-Ayland         /* IO_COMMAND of CPU with client_id bits */
2490db9350eSMark Cave-Ayland         s->toc_addr = 0xFFFA0030 | (val & 0x1e000);
2500db9350eSMark Cave-Ayland         break;
2510db9350eSMark Cave-Ayland 
2520db9350eSMark Cave-Ayland     case DINO_ILR:
2530db9350eSMark Cave-Ayland     case DINO_IRR0:
2540db9350eSMark Cave-Ayland     case DINO_IRR1:
2550db9350eSMark Cave-Ayland         /* These registers are read-only.  */
2560db9350eSMark Cave-Ayland         break;
2570db9350eSMark Cave-Ayland 
2580db9350eSMark Cave-Ayland     case DINO_GMASK ... DINO_TLTIM:
2590db9350eSMark Cave-Ayland         i = (addr - DINO_GMASK) / 4;
2600db9350eSMark Cave-Ayland         val &= reg800_keep_bits[i];
2610db9350eSMark Cave-Ayland         s->reg800[i] = val;
2620db9350eSMark Cave-Ayland         break;
2630db9350eSMark Cave-Ayland 
2640db9350eSMark Cave-Ayland     default:
2650db9350eSMark Cave-Ayland         /* Controlled by dino_chip_mem_valid above.  */
2660db9350eSMark Cave-Ayland         g_assert_not_reached();
2670db9350eSMark Cave-Ayland     }
2680db9350eSMark Cave-Ayland     return MEMTX_OK;
2690db9350eSMark Cave-Ayland }
2700db9350eSMark Cave-Ayland 
2710db9350eSMark Cave-Ayland static const MemoryRegionOps dino_chip_ops = {
2720db9350eSMark Cave-Ayland     .read_with_attrs = dino_chip_read_with_attrs,
2730db9350eSMark Cave-Ayland     .write_with_attrs = dino_chip_write_with_attrs,
2740db9350eSMark Cave-Ayland     .endianness = DEVICE_BIG_ENDIAN,
2750db9350eSMark Cave-Ayland     .valid = {
2760db9350eSMark Cave-Ayland         .min_access_size = 1,
2770db9350eSMark Cave-Ayland         .max_access_size = 4,
2780db9350eSMark Cave-Ayland         .accepts = dino_chip_mem_valid,
2790db9350eSMark Cave-Ayland     },
2800db9350eSMark Cave-Ayland     .impl = {
2810db9350eSMark Cave-Ayland         .min_access_size = 1,
2820db9350eSMark Cave-Ayland         .max_access_size = 4,
2830db9350eSMark Cave-Ayland     },
2840db9350eSMark Cave-Ayland };
2850db9350eSMark Cave-Ayland 
2860db9350eSMark Cave-Ayland static const VMStateDescription vmstate_dino = {
2870db9350eSMark Cave-Ayland     .name = "Dino",
2880db9350eSMark Cave-Ayland     .version_id = 2,
2890db9350eSMark Cave-Ayland     .minimum_version_id = 1,
290e2bd53a3SRichard Henderson     .fields = (const VMStateField[]) {
2910db9350eSMark Cave-Ayland         VMSTATE_UINT32(iar0, DinoState),
2920db9350eSMark Cave-Ayland         VMSTATE_UINT32(iar1, DinoState),
2930db9350eSMark Cave-Ayland         VMSTATE_UINT32(imr, DinoState),
2940db9350eSMark Cave-Ayland         VMSTATE_UINT32(ipr, DinoState),
2950db9350eSMark Cave-Ayland         VMSTATE_UINT32(icr, DinoState),
2960db9350eSMark Cave-Ayland         VMSTATE_UINT32(ilr, DinoState),
2970db9350eSMark Cave-Ayland         VMSTATE_UINT32(io_fbb_en, DinoState),
2980db9350eSMark Cave-Ayland         VMSTATE_UINT32(io_addr_en, DinoState),
2990db9350eSMark Cave-Ayland         VMSTATE_UINT32(io_control, DinoState),
3000db9350eSMark Cave-Ayland         VMSTATE_UINT32(toc_addr, DinoState),
3010db9350eSMark Cave-Ayland         VMSTATE_END_OF_LIST()
3020db9350eSMark Cave-Ayland     }
3030db9350eSMark Cave-Ayland };
3040db9350eSMark Cave-Ayland 
3050db9350eSMark Cave-Ayland /* Unlike pci_config_data_le_ops, no check of high bit set in config_reg.  */
3060db9350eSMark Cave-Ayland 
dino_config_data_read(void * opaque,hwaddr addr,unsigned len)3070db9350eSMark Cave-Ayland static uint64_t dino_config_data_read(void *opaque, hwaddr addr, unsigned len)
3080db9350eSMark Cave-Ayland {
3090db9350eSMark Cave-Ayland     PCIHostState *s = opaque;
3100db9350eSMark Cave-Ayland     return pci_data_read(s->bus, s->config_reg | (addr & 3), len);
3110db9350eSMark Cave-Ayland }
3120db9350eSMark Cave-Ayland 
dino_config_data_write(void * opaque,hwaddr addr,uint64_t val,unsigned len)3130db9350eSMark Cave-Ayland static void dino_config_data_write(void *opaque, hwaddr addr,
3140db9350eSMark Cave-Ayland                                    uint64_t val, unsigned len)
3150db9350eSMark Cave-Ayland {
3160db9350eSMark Cave-Ayland     PCIHostState *s = opaque;
3170db9350eSMark Cave-Ayland     pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
3180db9350eSMark Cave-Ayland }
3190db9350eSMark Cave-Ayland 
3200db9350eSMark Cave-Ayland static const MemoryRegionOps dino_config_data_ops = {
3210db9350eSMark Cave-Ayland     .read = dino_config_data_read,
3220db9350eSMark Cave-Ayland     .write = dino_config_data_write,
3230db9350eSMark Cave-Ayland     .endianness = DEVICE_LITTLE_ENDIAN,
3240db9350eSMark Cave-Ayland };
3250db9350eSMark Cave-Ayland 
dino_config_addr_read(void * opaque,hwaddr addr,unsigned len)3260db9350eSMark Cave-Ayland static uint64_t dino_config_addr_read(void *opaque, hwaddr addr, unsigned len)
3270db9350eSMark Cave-Ayland {
3280db9350eSMark Cave-Ayland     DinoState *s = opaque;
3290db9350eSMark Cave-Ayland     return s->config_reg_dino;
3300db9350eSMark Cave-Ayland }
3310db9350eSMark Cave-Ayland 
dino_config_addr_write(void * opaque,hwaddr addr,uint64_t val,unsigned len)3320db9350eSMark Cave-Ayland static void dino_config_addr_write(void *opaque, hwaddr addr,
3330db9350eSMark Cave-Ayland                                    uint64_t val, unsigned len)
3340db9350eSMark Cave-Ayland {
3350db9350eSMark Cave-Ayland     PCIHostState *s = opaque;
3360db9350eSMark Cave-Ayland     DinoState *ds = opaque;
3370db9350eSMark Cave-Ayland     ds->config_reg_dino = val; /* keep a copy of original value */
3380db9350eSMark Cave-Ayland     s->config_reg = val & ~3U;
3390db9350eSMark Cave-Ayland }
3400db9350eSMark Cave-Ayland 
3410db9350eSMark Cave-Ayland static const MemoryRegionOps dino_config_addr_ops = {
3420db9350eSMark Cave-Ayland     .read = dino_config_addr_read,
3430db9350eSMark Cave-Ayland     .write = dino_config_addr_write,
3440db9350eSMark Cave-Ayland     .valid.min_access_size = 4,
3450db9350eSMark Cave-Ayland     .valid.max_access_size = 4,
3460db9350eSMark Cave-Ayland     .endianness = DEVICE_BIG_ENDIAN,
3470db9350eSMark Cave-Ayland };
3480db9350eSMark Cave-Ayland 
dino_pcihost_set_iommu(PCIBus * bus,void * opaque,int devfn)3490db9350eSMark Cave-Ayland static AddressSpace *dino_pcihost_set_iommu(PCIBus *bus, void *opaque,
3500db9350eSMark Cave-Ayland                                             int devfn)
3510db9350eSMark Cave-Ayland {
3520db9350eSMark Cave-Ayland     DinoState *s = opaque;
3530db9350eSMark Cave-Ayland 
3540db9350eSMark Cave-Ayland     return &s->bm_as;
3550db9350eSMark Cave-Ayland }
3560db9350eSMark Cave-Ayland 
357ba7d12ebSYi Liu static const PCIIOMMUOps dino_iommu_ops = {
358ba7d12ebSYi Liu     .get_address_space = dino_pcihost_set_iommu,
359ba7d12ebSYi Liu };
360ba7d12ebSYi Liu 
3610db9350eSMark Cave-Ayland /*
3620db9350eSMark Cave-Ayland  * Dino interrupts are connected as shown on Page 78, Table 23
3630db9350eSMark Cave-Ayland  * (Little-endian bit numbers)
3640db9350eSMark Cave-Ayland  *    0   PCI INTA
3650db9350eSMark Cave-Ayland  *    1   PCI INTB
3660db9350eSMark Cave-Ayland  *    2   PCI INTC
3670db9350eSMark Cave-Ayland  *    3   PCI INTD
3680db9350eSMark Cave-Ayland  *    4   PCI INTE
3690db9350eSMark Cave-Ayland  *    5   PCI INTF
3700db9350eSMark Cave-Ayland  *    6   GSC External Interrupt
3710db9350eSMark Cave-Ayland  *    7   Bus Error for "less than fatal" mode
3720db9350eSMark Cave-Ayland  *    8   PS2
3730db9350eSMark Cave-Ayland  *    9   Unused
3740db9350eSMark Cave-Ayland  *    10  RS232
3750db9350eSMark Cave-Ayland  */
3760db9350eSMark Cave-Ayland 
dino_set_irq(void * opaque,int irq,int level)3770db9350eSMark Cave-Ayland static void dino_set_irq(void *opaque, int irq, int level)
3780db9350eSMark Cave-Ayland {
3790db9350eSMark Cave-Ayland     DinoState *s = opaque;
3800db9350eSMark Cave-Ayland     uint32_t bit = 1u << irq;
3810db9350eSMark Cave-Ayland     uint32_t old_ilr = s->ilr;
3820db9350eSMark Cave-Ayland 
3830db9350eSMark Cave-Ayland     if (level) {
3840db9350eSMark Cave-Ayland         uint32_t ena = bit & ~old_ilr;
3850db9350eSMark Cave-Ayland         s->ipr |= ena;
3860db9350eSMark Cave-Ayland         s->ilr = old_ilr | bit;
3870db9350eSMark Cave-Ayland         if (ena & s->imr) {
3880db9350eSMark Cave-Ayland             uint32_t iar = (ena & s->icr ? s->iar1 : s->iar0);
3890db9350eSMark Cave-Ayland             stl_be_phys(&address_space_memory, iar & -32, iar & 31);
3900db9350eSMark Cave-Ayland         }
3910db9350eSMark Cave-Ayland     } else {
3920db9350eSMark Cave-Ayland         s->ilr = old_ilr & ~bit;
3930db9350eSMark Cave-Ayland     }
3940db9350eSMark Cave-Ayland }
3950db9350eSMark Cave-Ayland 
dino_pci_map_irq(PCIDevice * d,int irq_num)3960db9350eSMark Cave-Ayland static int dino_pci_map_irq(PCIDevice *d, int irq_num)
3970db9350eSMark Cave-Ayland {
3980db9350eSMark Cave-Ayland     int slot = PCI_SLOT(d->devfn);
3990db9350eSMark Cave-Ayland 
4000db9350eSMark Cave-Ayland     assert(irq_num >= 0 && irq_num <= 3);
4010db9350eSMark Cave-Ayland 
4020db9350eSMark Cave-Ayland     return slot & 0x03;
4030db9350eSMark Cave-Ayland }
4040db9350eSMark Cave-Ayland 
dino_pcihost_reset(DeviceState * dev)4050db9350eSMark Cave-Ayland static void dino_pcihost_reset(DeviceState *dev)
4060db9350eSMark Cave-Ayland {
4070db9350eSMark Cave-Ayland     DinoState *s = DINO_PCI_HOST_BRIDGE(dev);
4080db9350eSMark Cave-Ayland 
4090db9350eSMark Cave-Ayland     s->iar0 = s->iar1 = 0xFFFB0000 + 3; /* CPU_HPA + 3 */
4100db9350eSMark Cave-Ayland     s->toc_addr = 0xFFFA0030; /* IO_COMMAND of CPU */
4110db9350eSMark Cave-Ayland }
4120db9350eSMark Cave-Ayland 
dino_pcihost_realize(DeviceState * dev,Error ** errp)4130db9350eSMark Cave-Ayland static void dino_pcihost_realize(DeviceState *dev, Error **errp)
4140db9350eSMark Cave-Ayland {
4150db9350eSMark Cave-Ayland     DinoState *s = DINO_PCI_HOST_BRIDGE(dev);
4160db9350eSMark Cave-Ayland 
4170db9350eSMark Cave-Ayland     /* Set up PCI view of memory: Bus master address space.  */
4180db9350eSMark Cave-Ayland     memory_region_init(&s->bm, OBJECT(s), "bm-dino", 4 * GiB);
4190db9350eSMark Cave-Ayland     memory_region_init_alias(&s->bm_ram_alias, OBJECT(s),
4200db9350eSMark Cave-Ayland                              "bm-system", s->memory_as, 0,
4210db9350eSMark Cave-Ayland                              0xf0000000 + DINO_MEM_CHUNK_SIZE);
4220db9350eSMark Cave-Ayland     memory_region_init_alias(&s->bm_pci_alias, OBJECT(s),
4230db9350eSMark Cave-Ayland                              "bm-pci", &s->pci_mem,
4240db9350eSMark Cave-Ayland                              0xf0000000 + DINO_MEM_CHUNK_SIZE,
4250db9350eSMark Cave-Ayland                              30 * DINO_MEM_CHUNK_SIZE);
4260db9350eSMark Cave-Ayland     memory_region_init_alias(&s->bm_cpu_alias, OBJECT(s),
4270db9350eSMark Cave-Ayland                              "bm-cpu", s->memory_as, 0xfff00000,
4280db9350eSMark Cave-Ayland                              0xfffff);
4290db9350eSMark Cave-Ayland     memory_region_add_subregion(&s->bm, 0,
4300db9350eSMark Cave-Ayland                                 &s->bm_ram_alias);
4310db9350eSMark Cave-Ayland     memory_region_add_subregion(&s->bm,
4320db9350eSMark Cave-Ayland                                 0xf0000000 + DINO_MEM_CHUNK_SIZE,
4330db9350eSMark Cave-Ayland                                 &s->bm_pci_alias);
4340db9350eSMark Cave-Ayland     memory_region_add_subregion(&s->bm, 0xfff00000,
4350db9350eSMark Cave-Ayland                                 &s->bm_cpu_alias);
4360db9350eSMark Cave-Ayland 
4370db9350eSMark Cave-Ayland     address_space_init(&s->bm_as, &s->bm, "pci-bm");
4380db9350eSMark Cave-Ayland }
4390db9350eSMark Cave-Ayland 
dino_pcihost_unrealize(DeviceState * dev)4400db9350eSMark Cave-Ayland static void dino_pcihost_unrealize(DeviceState *dev)
4410db9350eSMark Cave-Ayland {
4420db9350eSMark Cave-Ayland     DinoState *s = DINO_PCI_HOST_BRIDGE(dev);
4430db9350eSMark Cave-Ayland 
4440db9350eSMark Cave-Ayland     address_space_destroy(&s->bm_as);
4450db9350eSMark Cave-Ayland }
4460db9350eSMark Cave-Ayland 
dino_pcihost_init(Object * obj)4470db9350eSMark Cave-Ayland static void dino_pcihost_init(Object *obj)
4480db9350eSMark Cave-Ayland {
4490db9350eSMark Cave-Ayland     DinoState *s = DINO_PCI_HOST_BRIDGE(obj);
4500db9350eSMark Cave-Ayland     PCIHostState *phb = PCI_HOST_BRIDGE(obj);
4510db9350eSMark Cave-Ayland     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
4520db9350eSMark Cave-Ayland     int i;
4530db9350eSMark Cave-Ayland 
4540db9350eSMark Cave-Ayland     /* Dino PCI access from main memory.  */
4550db9350eSMark Cave-Ayland     memory_region_init_io(&s->this_mem, OBJECT(s), &dino_chip_ops,
4560db9350eSMark Cave-Ayland                           s, "dino", 4096);
4570db9350eSMark Cave-Ayland 
4580db9350eSMark Cave-Ayland     /* Dino PCI config. */
4590db9350eSMark Cave-Ayland     memory_region_init_io(&phb->conf_mem, OBJECT(phb),
4600db9350eSMark Cave-Ayland                           &dino_config_addr_ops, DEVICE(s),
4610db9350eSMark Cave-Ayland                           "pci-conf-idx", 4);
4620db9350eSMark Cave-Ayland     memory_region_init_io(&phb->data_mem, OBJECT(phb),
4630db9350eSMark Cave-Ayland                           &dino_config_data_ops, DEVICE(s),
4640db9350eSMark Cave-Ayland                           "pci-conf-data", 4);
4650db9350eSMark Cave-Ayland     memory_region_add_subregion(&s->this_mem, DINO_PCI_CONFIG_ADDR,
4660db9350eSMark Cave-Ayland                                 &phb->conf_mem);
4670db9350eSMark Cave-Ayland     memory_region_add_subregion(&s->this_mem, DINO_CONFIG_DATA,
4680db9350eSMark Cave-Ayland                                 &phb->data_mem);
4690db9350eSMark Cave-Ayland 
4700db9350eSMark Cave-Ayland     /* Dino PCI bus memory.  */
4710db9350eSMark Cave-Ayland     memory_region_init(&s->pci_mem, OBJECT(s), "pci-memory", 4 * GiB);
4720db9350eSMark Cave-Ayland 
4730db9350eSMark Cave-Ayland     phb->bus = pci_register_root_bus(DEVICE(s), "pci",
4740db9350eSMark Cave-Ayland                                      dino_set_irq, dino_pci_map_irq, s,
4750db9350eSMark Cave-Ayland                                      &s->pci_mem, get_system_io(),
4760db9350eSMark Cave-Ayland                                      PCI_DEVFN(0, 0), 32, TYPE_PCI_BUS);
4770db9350eSMark Cave-Ayland 
4780db9350eSMark Cave-Ayland     /* Set up windows into PCI bus memory.  */
4790db9350eSMark Cave-Ayland     for (i = 1; i < 31; i++) {
4800db9350eSMark Cave-Ayland         uint32_t addr = 0xf0000000 + i * DINO_MEM_CHUNK_SIZE;
4810db9350eSMark Cave-Ayland         char *name = g_strdup_printf("PCI Outbound Window %d", i);
4820db9350eSMark Cave-Ayland         memory_region_init_alias(&s->pci_mem_alias[i], OBJECT(s),
4830db9350eSMark Cave-Ayland                                  name, &s->pci_mem, addr,
4840db9350eSMark Cave-Ayland                                  DINO_MEM_CHUNK_SIZE);
4850db9350eSMark Cave-Ayland         g_free(name);
4860db9350eSMark Cave-Ayland     }
4870db9350eSMark Cave-Ayland 
488ba7d12ebSYi Liu     pci_setup_iommu(phb->bus, &dino_iommu_ops, s);
4890db9350eSMark Cave-Ayland 
4900db9350eSMark Cave-Ayland     sysbus_init_mmio(sbd, &s->this_mem);
4910db9350eSMark Cave-Ayland 
4920db9350eSMark Cave-Ayland     qdev_init_gpio_in(DEVICE(obj), dino_set_irq, DINO_IRQS);
4930db9350eSMark Cave-Ayland }
4940db9350eSMark Cave-Ayland 
4950db9350eSMark Cave-Ayland static Property dino_pcihost_properties[] = {
4960db9350eSMark Cave-Ayland     DEFINE_PROP_LINK("memory-as", DinoState, memory_as, TYPE_MEMORY_REGION,
4970db9350eSMark Cave-Ayland                      MemoryRegion *),
4980db9350eSMark Cave-Ayland     DEFINE_PROP_END_OF_LIST(),
4990db9350eSMark Cave-Ayland };
5000db9350eSMark Cave-Ayland 
dino_pcihost_class_init(ObjectClass * klass,void * data)5010db9350eSMark Cave-Ayland static void dino_pcihost_class_init(ObjectClass *klass, void *data)
5020db9350eSMark Cave-Ayland {
5030db9350eSMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(klass);
5040db9350eSMark Cave-Ayland 
505*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, dino_pcihost_reset);
5060db9350eSMark Cave-Ayland     dc->realize = dino_pcihost_realize;
5070db9350eSMark Cave-Ayland     dc->unrealize = dino_pcihost_unrealize;
5080db9350eSMark Cave-Ayland     device_class_set_props(dc, dino_pcihost_properties);
5090db9350eSMark Cave-Ayland     dc->vmsd = &vmstate_dino;
5100db9350eSMark Cave-Ayland }
5110db9350eSMark Cave-Ayland 
5120db9350eSMark Cave-Ayland static const TypeInfo dino_pcihost_info = {
5130db9350eSMark Cave-Ayland     .name          = TYPE_DINO_PCI_HOST_BRIDGE,
5140db9350eSMark Cave-Ayland     .parent        = TYPE_PCI_HOST_BRIDGE,
5150db9350eSMark Cave-Ayland     .instance_init = dino_pcihost_init,
5160db9350eSMark Cave-Ayland     .instance_size = sizeof(DinoState),
5170db9350eSMark Cave-Ayland     .class_init    = dino_pcihost_class_init,
5180db9350eSMark Cave-Ayland };
5190db9350eSMark Cave-Ayland 
dino_register_types(void)5200db9350eSMark Cave-Ayland static void dino_register_types(void)
5210db9350eSMark Cave-Ayland {
5220db9350eSMark Cave-Ayland     type_register_static(&dino_pcihost_info);
5230db9350eSMark Cave-Ayland }
5240db9350eSMark Cave-Ayland 
5250db9350eSMark Cave-Ayland type_init(dino_register_types)
526