xref: /openbmc/qemu/hw/pci-host/bonito.c (revision 83baec642a13a69398a2643a1f905606c13cd363)
1c0907c9eSPaolo Bonzini /*
2c0907c9eSPaolo Bonzini  * bonito north bridge support
3c0907c9eSPaolo Bonzini  *
4c0907c9eSPaolo Bonzini  * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
5c0907c9eSPaolo Bonzini  * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
6c0907c9eSPaolo Bonzini  *
7c0907c9eSPaolo Bonzini  * This code is licensed under the GNU GPL v2.
8c0907c9eSPaolo Bonzini  *
9c0907c9eSPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
10c0907c9eSPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
11c0907c9eSPaolo Bonzini  */
12c0907c9eSPaolo Bonzini 
13c0907c9eSPaolo Bonzini /*
14c3a09ff6SPhilippe Mathieu-Daudé  * fuloong 2e mini pc has a bonito north bridge.
15c0907c9eSPaolo Bonzini  */
16c0907c9eSPaolo Bonzini 
17f3db354cSFilip Bozuta /*
18f3db354cSFilip Bozuta  * what is the meaning of devfn in qemu and IDSEL in bonito northbridge?
19c0907c9eSPaolo Bonzini  *
20c0907c9eSPaolo Bonzini  * devfn   pci_slot<<3  + funno
21c0907c9eSPaolo Bonzini  * one pci bus can have 32 devices and each device can have 8 functions.
22c0907c9eSPaolo Bonzini  *
23c0907c9eSPaolo Bonzini  * In bonito north bridge, pci slot = IDSEL bit - 12.
24c0907c9eSPaolo Bonzini  * For example, PCI_IDSEL_VIA686B = 17,
25c0907c9eSPaolo Bonzini  * pci slot = 17-12=5
26c0907c9eSPaolo Bonzini  *
27c0907c9eSPaolo Bonzini  * so
28c0907c9eSPaolo Bonzini  * VT686B_FUN0's devfn = (5<<3)+0
29c0907c9eSPaolo Bonzini  * VT686B_FUN1's devfn = (5<<3)+1
30c0907c9eSPaolo Bonzini  *
31c0907c9eSPaolo Bonzini  * qemu also uses pci address for north bridge to access pci config register.
32c0907c9eSPaolo Bonzini  * bus_no   [23:16]
33c0907c9eSPaolo Bonzini  * dev_no   [15:11]
34c0907c9eSPaolo Bonzini  * fun_no   [10:8]
35c0907c9eSPaolo Bonzini  * reg_no   [7:2]
36c0907c9eSPaolo Bonzini  *
37c0907c9eSPaolo Bonzini  * so function bonito_sbridge_pciaddr for the translation from
38c0907c9eSPaolo Bonzini  * north bridge address to pci address.
39c0907c9eSPaolo Bonzini  */
40c0907c9eSPaolo Bonzini 
4197d5408fSPeter Maydell #include "qemu/osdep.h"
42a0b544c1SPhilippe Mathieu-Daudé #include "qemu/units.h"
433e80f690SMarkus Armbruster #include "qapi/error.h"
440151abe4SAlistair Francis #include "qemu/error-report.h"
45edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
4664552b6bSMarkus Armbruster #include "hw/irq.h"
47c0907c9eSPaolo Bonzini #include "hw/mips/mips.h"
48aad07969SPhilippe Mathieu-Daudé #include "hw/pci-host/bonito.h"
49c0907c9eSPaolo Bonzini #include "hw/pci/pci_host.h"
50d6454270SMarkus Armbruster #include "migration/vmstate.h"
5154d31236SMarkus Armbruster #include "sysemu/runstate.h"
5225cca0a9SPhilippe Mathieu-Daudé #include "hw/misc/unimp.h"
531f8a6c8bSPhilippe Mathieu-Daudé #include "hw/registerfields.h"
54db1015e9SEduardo Habkost #include "qom/object.h"
55300491f9SPhilippe Mathieu-Daudé #include "trace.h"
56c0907c9eSPaolo Bonzini 
57f3db354cSFilip Bozuta /* #define DEBUG_BONITO */
58c0907c9eSPaolo Bonzini 
59c0907c9eSPaolo Bonzini #ifdef DEBUG_BONITO
60a89f364aSAlistair Francis #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__)
61c0907c9eSPaolo Bonzini #else
62c0907c9eSPaolo Bonzini #define DPRINTF(fmt, ...)
63c0907c9eSPaolo Bonzini #endif
64c0907c9eSPaolo Bonzini 
65f1c0cff8SMichael Tokarev /* from linux source code. include/asm-mips/mips-boards/bonito64.h*/
66c0907c9eSPaolo Bonzini #define BONITO_BOOT_BASE        0x1fc00000
67c0907c9eSPaolo Bonzini #define BONITO_BOOT_SIZE        0x00100000
68c0907c9eSPaolo Bonzini #define BONITO_BOOT_TOP         (BONITO_BOOT_BASE + BONITO_BOOT_SIZE - 1)
69c0907c9eSPaolo Bonzini #define BONITO_FLASH_BASE       0x1c000000
70c0907c9eSPaolo Bonzini #define BONITO_FLASH_SIZE       0x03000000
71c0907c9eSPaolo Bonzini #define BONITO_FLASH_TOP        (BONITO_FLASH_BASE + BONITO_FLASH_SIZE - 1)
72c0907c9eSPaolo Bonzini #define BONITO_SOCKET_BASE      0x1f800000
73c0907c9eSPaolo Bonzini #define BONITO_SOCKET_SIZE      0x00400000
74c0907c9eSPaolo Bonzini #define BONITO_SOCKET_TOP       (BONITO_SOCKET_BASE + BONITO_SOCKET_SIZE - 1)
75c0907c9eSPaolo Bonzini #define BONITO_REG_BASE         0x1fe00000
76c0907c9eSPaolo Bonzini #define BONITO_REG_SIZE         0x00040000
77c0907c9eSPaolo Bonzini #define BONITO_REG_TOP          (BONITO_REG_BASE + BONITO_REG_SIZE - 1)
78c0907c9eSPaolo Bonzini #define BONITO_DEV_BASE         0x1ff00000
79c0907c9eSPaolo Bonzini #define BONITO_DEV_SIZE         0x00100000
80c0907c9eSPaolo Bonzini #define BONITO_DEV_TOP          (BONITO_DEV_BASE + BONITO_DEV_SIZE - 1)
81c0907c9eSPaolo Bonzini #define BONITO_PCILO_BASE       0x10000000
82c0907c9eSPaolo Bonzini #define BONITO_PCILO_BASE_VA    0xb0000000
83c0907c9eSPaolo Bonzini #define BONITO_PCILO_SIZE       0x0c000000
84c0907c9eSPaolo Bonzini #define BONITO_PCILO_TOP        (BONITO_PCILO_BASE + BONITO_PCILO_SIZE - 1)
85c0907c9eSPaolo Bonzini #define BONITO_PCILO0_BASE      0x10000000
86c0907c9eSPaolo Bonzini #define BONITO_PCILO1_BASE      0x14000000
87c0907c9eSPaolo Bonzini #define BONITO_PCILO2_BASE      0x18000000
88c0907c9eSPaolo Bonzini #define BONITO_PCIHI_BASE       0x20000000
89a0b544c1SPhilippe Mathieu-Daudé #define BONITO_PCIHI_SIZE       0x60000000
90c0907c9eSPaolo Bonzini #define BONITO_PCIHI_TOP        (BONITO_PCIHI_BASE + BONITO_PCIHI_SIZE - 1)
91c0907c9eSPaolo Bonzini #define BONITO_PCIIO_BASE       0x1fd00000
92c0907c9eSPaolo Bonzini #define BONITO_PCIIO_BASE_VA    0xbfd00000
93c0907c9eSPaolo Bonzini #define BONITO_PCIIO_SIZE       0x00010000
94c0907c9eSPaolo Bonzini #define BONITO_PCIIO_TOP        (BONITO_PCIIO_BASE + BONITO_PCIIO_SIZE - 1)
95c0907c9eSPaolo Bonzini #define BONITO_PCICFG_BASE      0x1fe80000
96c0907c9eSPaolo Bonzini #define BONITO_PCICFG_SIZE      0x00080000
97c0907c9eSPaolo Bonzini #define BONITO_PCICFG_TOP       (BONITO_PCICFG_BASE + BONITO_PCICFG_SIZE - 1)
98c0907c9eSPaolo Bonzini 
99c0907c9eSPaolo Bonzini 
100c0907c9eSPaolo Bonzini #define BONITO_PCICONFIGBASE    0x00
101c0907c9eSPaolo Bonzini #define BONITO_REGBASE          0x100
102c0907c9eSPaolo Bonzini 
103c0907c9eSPaolo Bonzini #define BONITO_PCICONFIG_BASE   (BONITO_PCICONFIGBASE + BONITO_REG_BASE)
104c0907c9eSPaolo Bonzini #define BONITO_PCICONFIG_SIZE   (0x100)
105c0907c9eSPaolo Bonzini 
106c0907c9eSPaolo Bonzini #define BONITO_INTERNAL_REG_BASE  (BONITO_REGBASE + BONITO_REG_BASE)
107c0907c9eSPaolo Bonzini #define BONITO_INTERNAL_REG_SIZE  (0x70)
108c0907c9eSPaolo Bonzini 
109c0907c9eSPaolo Bonzini #define BONITO_SPCICONFIG_BASE  (BONITO_PCICFG_BASE)
110c0907c9eSPaolo Bonzini #define BONITO_SPCICONFIG_SIZE  (BONITO_PCICFG_SIZE)
111c0907c9eSPaolo Bonzini 
112c0907c9eSPaolo Bonzini 
113c0907c9eSPaolo Bonzini 
114c0907c9eSPaolo Bonzini /* 1. Bonito h/w Configuration */
115c0907c9eSPaolo Bonzini /* Power on register */
116c0907c9eSPaolo Bonzini 
117c0907c9eSPaolo Bonzini #define BONITO_BONPONCFG        (0x00 >> 2)      /* 0x100 */
1181f8a6c8bSPhilippe Mathieu-Daudé 
1191f8a6c8bSPhilippe Mathieu-Daudé /* PCI configuration register */
120c0907c9eSPaolo Bonzini #define BONITO_BONGENCFG_OFFSET 0x4
121c0907c9eSPaolo Bonzini #define BONITO_BONGENCFG        (BONITO_BONGENCFG_OFFSET >> 2)   /*0x104 */
1221f8a6c8bSPhilippe Mathieu-Daudé REG32(BONGENCFG,        0x104)
1231f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, DEBUGMODE,      0, 1)
1241f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, SNOOP,          1, 1)
1251f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, CPUSELFRESET,   2, 1)
1261f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, BYTESWAP,       6, 1)
1271f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, UNCACHED,       7, 1)
1281f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, PREFETCH,       8, 1)
1291f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, WRITEBEHIND,    9, 1)
1301f8a6c8bSPhilippe Mathieu-Daudé FIELD(BONGENCFG, PCIQUEUE,      12, 1)
131c0907c9eSPaolo Bonzini 
132c0907c9eSPaolo Bonzini /* 2. IO & IDE configuration */
133c0907c9eSPaolo Bonzini #define BONITO_IODEVCFG         (0x08 >> 2)      /* 0x108 */
134c0907c9eSPaolo Bonzini 
135c0907c9eSPaolo Bonzini /* 3. IO & IDE configuration */
136c0907c9eSPaolo Bonzini #define BONITO_SDCFG            (0x0c >> 2)      /* 0x10c */
137c0907c9eSPaolo Bonzini 
138c0907c9eSPaolo Bonzini /* 4. PCI address map control */
139c0907c9eSPaolo Bonzini #define BONITO_PCIMAP           (0x10 >> 2)      /* 0x110 */
140c0907c9eSPaolo Bonzini #define BONITO_PCIMEMBASECFG    (0x14 >> 2)      /* 0x114 */
141c0907c9eSPaolo Bonzini #define BONITO_PCIMAP_CFG       (0x18 >> 2)      /* 0x118 */
142c0907c9eSPaolo Bonzini 
143c0907c9eSPaolo Bonzini /* 5. ICU & GPIO regs */
144c0907c9eSPaolo Bonzini /* GPIO Regs - r/w */
145c0907c9eSPaolo Bonzini #define BONITO_GPIODATA_OFFSET  0x1c
146c0907c9eSPaolo Bonzini #define BONITO_GPIODATA         (BONITO_GPIODATA_OFFSET >> 2)   /* 0x11c */
147c0907c9eSPaolo Bonzini #define BONITO_GPIOIE           (0x20 >> 2)      /* 0x120 */
148c0907c9eSPaolo Bonzini 
149c0907c9eSPaolo Bonzini /* ICU Configuration Regs - r/w */
150c0907c9eSPaolo Bonzini #define BONITO_INTEDGE          (0x24 >> 2)      /* 0x124 */
151c0907c9eSPaolo Bonzini #define BONITO_INTSTEER         (0x28 >> 2)      /* 0x128 */
152c0907c9eSPaolo Bonzini #define BONITO_INTPOL           (0x2c >> 2)      /* 0x12c */
153c0907c9eSPaolo Bonzini 
154c0907c9eSPaolo Bonzini /* ICU Enable Regs - IntEn & IntISR are r/o. */
155c0907c9eSPaolo Bonzini #define BONITO_INTENSET         (0x30 >> 2)      /* 0x130 */
156c0907c9eSPaolo Bonzini #define BONITO_INTENCLR         (0x34 >> 2)      /* 0x134 */
157c0907c9eSPaolo Bonzini #define BONITO_INTEN            (0x38 >> 2)      /* 0x138 */
158c0907c9eSPaolo Bonzini #define BONITO_INTISR           (0x3c >> 2)      /* 0x13c */
159c0907c9eSPaolo Bonzini 
160c0907c9eSPaolo Bonzini /* PCI mail boxes */
161c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL0_OFFSET    0x40
162c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL1_OFFSET    0x44
163c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL2_OFFSET    0x48
164c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL3_OFFSET    0x4c
165c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL0         (0x40 >> 2)      /* 0x140 */
166c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL1         (0x44 >> 2)      /* 0x144 */
167c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL2         (0x48 >> 2)      /* 0x148 */
168c0907c9eSPaolo Bonzini #define BONITO_PCIMAIL3         (0x4c >> 2)      /* 0x14c */
169c0907c9eSPaolo Bonzini 
170c0907c9eSPaolo Bonzini /* 6. PCI cache */
171c0907c9eSPaolo Bonzini #define BONITO_PCICACHECTRL     (0x50 >> 2)      /* 0x150 */
172c0907c9eSPaolo Bonzini #define BONITO_PCICACHETAG      (0x54 >> 2)      /* 0x154 */
173c0907c9eSPaolo Bonzini #define BONITO_PCIBADADDR       (0x58 >> 2)      /* 0x158 */
174c0907c9eSPaolo Bonzini #define BONITO_PCIMSTAT         (0x5c >> 2)      /* 0x15c */
175c0907c9eSPaolo Bonzini 
176c0907c9eSPaolo Bonzini /* 7. other*/
177c0907c9eSPaolo Bonzini #define BONITO_TIMECFG          (0x60 >> 2)      /* 0x160 */
178c0907c9eSPaolo Bonzini #define BONITO_CPUCFG           (0x64 >> 2)      /* 0x164 */
179c0907c9eSPaolo Bonzini #define BONITO_DQCFG            (0x68 >> 2)      /* 0x168 */
180c0907c9eSPaolo Bonzini #define BONITO_MEMSIZE          (0x6C >> 2)      /* 0x16c */
181c0907c9eSPaolo Bonzini 
182c0907c9eSPaolo Bonzini #define BONITO_REGS             (0x70 >> 2)
183c0907c9eSPaolo Bonzini 
184c0907c9eSPaolo Bonzini /* PCI config for south bridge. type 0 */
185c0907c9eSPaolo Bonzini #define BONITO_PCICONF_IDSEL_MASK      0xfffff800     /* [31:11] */
186c0907c9eSPaolo Bonzini #define BONITO_PCICONF_IDSEL_OFFSET    11
187c0907c9eSPaolo Bonzini #define BONITO_PCICONF_FUN_MASK        0x700    /* [10:8] */
188c0907c9eSPaolo Bonzini #define BONITO_PCICONF_FUN_OFFSET      8
189300491f9SPhilippe Mathieu-Daudé #define BONITO_PCICONF_REG_MASK_DS     (~3)         /* Per datasheet */
190711ef337SPhilippe Mathieu-Daudé #define BONITO_PCICONF_REG_MASK_HW     0xff         /* As seen running PMON */
191c0907c9eSPaolo Bonzini #define BONITO_PCICONF_REG_OFFSET      0
192c0907c9eSPaolo Bonzini 
193c0907c9eSPaolo Bonzini 
194c0907c9eSPaolo Bonzini /* idsel BIT = pci slot number +12 */
195c0907c9eSPaolo Bonzini #define PCI_SLOT_BASE              12
196c0907c9eSPaolo Bonzini #define PCI_IDSEL_VIA686B_BIT      (17)
197c0907c9eSPaolo Bonzini #define PCI_IDSEL_VIA686B          (1 << PCI_IDSEL_VIA686B_BIT)
198c0907c9eSPaolo Bonzini 
199c0907c9eSPaolo Bonzini #define PCI_ADDR(busno , devno , funno , regno)  \
2000374cbd2SPhilippe Mathieu-Daudé     ((PCI_BUILD_BDF(busno, PCI_DEVFN(devno , funno)) << 8) + (regno))
201c0907c9eSPaolo Bonzini 
202c0907c9eSPaolo Bonzini typedef struct BonitoState BonitoState;
203c0907c9eSPaolo Bonzini 
204db1015e9SEduardo Habkost struct PCIBonitoState {
205c0907c9eSPaolo Bonzini     PCIDevice dev;
206c0907c9eSPaolo Bonzini 
207c0907c9eSPaolo Bonzini     BonitoState *pcihost;
208c0907c9eSPaolo Bonzini     uint32_t regs[BONITO_REGS];
209c0907c9eSPaolo Bonzini 
210c0907c9eSPaolo Bonzini     struct bonldma {
211c0907c9eSPaolo Bonzini         uint32_t ldmactrl;
212c0907c9eSPaolo Bonzini         uint32_t ldmastat;
213c0907c9eSPaolo Bonzini         uint32_t ldmaaddr;
214c0907c9eSPaolo Bonzini         uint32_t ldmago;
215c0907c9eSPaolo Bonzini     } bonldma;
216c0907c9eSPaolo Bonzini 
217c0907c9eSPaolo Bonzini     /* Based at 1fe00300, bonito Copier */
218c0907c9eSPaolo Bonzini     struct boncop {
219c0907c9eSPaolo Bonzini         uint32_t copctrl;
220c0907c9eSPaolo Bonzini         uint32_t copstat;
221c0907c9eSPaolo Bonzini         uint32_t coppaddr;
222c0907c9eSPaolo Bonzini         uint32_t copgo;
223c0907c9eSPaolo Bonzini     } boncop;
224c0907c9eSPaolo Bonzini 
225c0907c9eSPaolo Bonzini     /* Bonito registers */
226c0907c9eSPaolo Bonzini     MemoryRegion iomem;
227c0907c9eSPaolo Bonzini     MemoryRegion iomem_ldma;
228c0907c9eSPaolo Bonzini     MemoryRegion iomem_cop;
229e37b80faSPaolo Bonzini     MemoryRegion bonito_pciio;
230e37b80faSPaolo Bonzini     MemoryRegion bonito_localio;
231c0907c9eSPaolo Bonzini 
232db1015e9SEduardo Habkost };
233db1015e9SEduardo Habkost typedef struct PCIBonitoState PCIBonitoState;
234c0907c9eSPaolo Bonzini 
235a2a645d9SCao jin struct BonitoState {
236a2a645d9SCao jin     PCIHostState parent_obj;
237a2a645d9SCao jin     qemu_irq *pic;
238a2a645d9SCao jin     PCIBonitoState *pci_dev;
239f7cf2219SBALATON Zoltan     MemoryRegion pci_mem;
240a2a645d9SCao jin };
241a2a645d9SCao jin 
242a2a645d9SCao jin #define TYPE_PCI_BONITO "Bonito"
OBJECT_DECLARE_SIMPLE_TYPE(PCIBonitoState,PCI_BONITO)2438063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(PCIBonitoState, PCI_BONITO)
244c0907c9eSPaolo Bonzini 
245c0907c9eSPaolo Bonzini static void bonito_writel(void *opaque, hwaddr addr,
246c0907c9eSPaolo Bonzini                           uint64_t val, unsigned size)
247c0907c9eSPaolo Bonzini {
248c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
249c0907c9eSPaolo Bonzini     uint32_t saddr;
250c0907c9eSPaolo Bonzini     int reset = 0;
251c0907c9eSPaolo Bonzini 
2520ca4f941SPaolo Bonzini     saddr = addr >> 2;
253c0907c9eSPaolo Bonzini 
254883f2c59SPhilippe Mathieu-Daudé     DPRINTF("bonito_writel "HWADDR_FMT_plx" val %lx saddr %x\n",
255f3db354cSFilip Bozuta             addr, val, saddr);
256c0907c9eSPaolo Bonzini     switch (saddr) {
257c0907c9eSPaolo Bonzini     case BONITO_BONPONCFG:
258c0907c9eSPaolo Bonzini     case BONITO_IODEVCFG:
259c0907c9eSPaolo Bonzini     case BONITO_SDCFG:
260c0907c9eSPaolo Bonzini     case BONITO_PCIMAP:
261c0907c9eSPaolo Bonzini     case BONITO_PCIMEMBASECFG:
262c0907c9eSPaolo Bonzini     case BONITO_PCIMAP_CFG:
263c0907c9eSPaolo Bonzini     case BONITO_GPIODATA:
264c0907c9eSPaolo Bonzini     case BONITO_GPIOIE:
265c0907c9eSPaolo Bonzini     case BONITO_INTEDGE:
266c0907c9eSPaolo Bonzini     case BONITO_INTSTEER:
267c0907c9eSPaolo Bonzini     case BONITO_INTPOL:
268c0907c9eSPaolo Bonzini     case BONITO_PCIMAIL0:
269c0907c9eSPaolo Bonzini     case BONITO_PCIMAIL1:
270c0907c9eSPaolo Bonzini     case BONITO_PCIMAIL2:
271c0907c9eSPaolo Bonzini     case BONITO_PCIMAIL3:
272c0907c9eSPaolo Bonzini     case BONITO_PCICACHECTRL:
273c0907c9eSPaolo Bonzini     case BONITO_PCICACHETAG:
274c0907c9eSPaolo Bonzini     case BONITO_PCIBADADDR:
275c0907c9eSPaolo Bonzini     case BONITO_PCIMSTAT:
276c0907c9eSPaolo Bonzini     case BONITO_TIMECFG:
277c0907c9eSPaolo Bonzini     case BONITO_CPUCFG:
278c0907c9eSPaolo Bonzini     case BONITO_DQCFG:
279c0907c9eSPaolo Bonzini     case BONITO_MEMSIZE:
280c0907c9eSPaolo Bonzini         s->regs[saddr] = val;
281c0907c9eSPaolo Bonzini         break;
282c0907c9eSPaolo Bonzini     case BONITO_BONGENCFG:
283c0907c9eSPaolo Bonzini         if (!(s->regs[saddr] & 0x04) && (val & 0x04)) {
284c0907c9eSPaolo Bonzini             reset = 1; /* bit 2 jump from 0 to 1 cause reset */
285c0907c9eSPaolo Bonzini         }
286c0907c9eSPaolo Bonzini         s->regs[saddr] = val;
287c0907c9eSPaolo Bonzini         if (reset) {
288cf83f140SEric Blake             qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
289c0907c9eSPaolo Bonzini         }
290c0907c9eSPaolo Bonzini         break;
291c0907c9eSPaolo Bonzini     case BONITO_INTENSET:
292c0907c9eSPaolo Bonzini         s->regs[BONITO_INTENSET] = val;
293c0907c9eSPaolo Bonzini         s->regs[BONITO_INTEN] |= val;
294c0907c9eSPaolo Bonzini         break;
295c0907c9eSPaolo Bonzini     case BONITO_INTENCLR:
296c0907c9eSPaolo Bonzini         s->regs[BONITO_INTENCLR] = val;
297c0907c9eSPaolo Bonzini         s->regs[BONITO_INTEN] &= ~val;
298c0907c9eSPaolo Bonzini         break;
299c0907c9eSPaolo Bonzini     case BONITO_INTEN:
300c0907c9eSPaolo Bonzini     case BONITO_INTISR:
301c0907c9eSPaolo Bonzini         DPRINTF("write to readonly bonito register %x\n", saddr);
302c0907c9eSPaolo Bonzini         break;
303c0907c9eSPaolo Bonzini     default:
304c0907c9eSPaolo Bonzini         DPRINTF("write to unknown bonito register %x\n", saddr);
305c0907c9eSPaolo Bonzini         break;
306c0907c9eSPaolo Bonzini     }
307c0907c9eSPaolo Bonzini }
308c0907c9eSPaolo Bonzini 
bonito_readl(void * opaque,hwaddr addr,unsigned size)309c0907c9eSPaolo Bonzini static uint64_t bonito_readl(void *opaque, hwaddr addr,
310c0907c9eSPaolo Bonzini                              unsigned size)
311c0907c9eSPaolo Bonzini {
312c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
313c0907c9eSPaolo Bonzini     uint32_t saddr;
314c0907c9eSPaolo Bonzini 
3150ca4f941SPaolo Bonzini     saddr = addr >> 2;
316c0907c9eSPaolo Bonzini 
317883f2c59SPhilippe Mathieu-Daudé     DPRINTF("bonito_readl "HWADDR_FMT_plx"\n", addr);
318c0907c9eSPaolo Bonzini     switch (saddr) {
319c0907c9eSPaolo Bonzini     case BONITO_INTISR:
320c0907c9eSPaolo Bonzini         return s->regs[saddr];
321c0907c9eSPaolo Bonzini     default:
322c0907c9eSPaolo Bonzini         return s->regs[saddr];
323c0907c9eSPaolo Bonzini     }
324c0907c9eSPaolo Bonzini }
325c0907c9eSPaolo Bonzini 
326c0907c9eSPaolo Bonzini static const MemoryRegionOps bonito_ops = {
327c0907c9eSPaolo Bonzini     .read = bonito_readl,
328c0907c9eSPaolo Bonzini     .write = bonito_writel,
329c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
330c0907c9eSPaolo Bonzini     .valid = {
331c0907c9eSPaolo Bonzini         .min_access_size = 4,
332c0907c9eSPaolo Bonzini         .max_access_size = 4,
333c0907c9eSPaolo Bonzini     },
334c0907c9eSPaolo Bonzini };
335c0907c9eSPaolo Bonzini 
bonito_pciconf_writel(void * opaque,hwaddr addr,uint64_t val,unsigned size)336c0907c9eSPaolo Bonzini static void bonito_pciconf_writel(void *opaque, hwaddr addr,
337c0907c9eSPaolo Bonzini                                   uint64_t val, unsigned size)
338c0907c9eSPaolo Bonzini {
339c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
340c0907c9eSPaolo Bonzini     PCIDevice *d = PCI_DEVICE(s);
341c0907c9eSPaolo Bonzini 
342883f2c59SPhilippe Mathieu-Daudé     DPRINTF("bonito_pciconf_writel "HWADDR_FMT_plx" val %lx\n", addr, val);
343c0907c9eSPaolo Bonzini     d->config_write(d, addr, val, 4);
344c0907c9eSPaolo Bonzini }
345c0907c9eSPaolo Bonzini 
bonito_pciconf_readl(void * opaque,hwaddr addr,unsigned size)346c0907c9eSPaolo Bonzini static uint64_t bonito_pciconf_readl(void *opaque, hwaddr addr,
347c0907c9eSPaolo Bonzini                                      unsigned size)
348c0907c9eSPaolo Bonzini {
349c0907c9eSPaolo Bonzini 
350c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
351c0907c9eSPaolo Bonzini     PCIDevice *d = PCI_DEVICE(s);
352c0907c9eSPaolo Bonzini 
353883f2c59SPhilippe Mathieu-Daudé     DPRINTF("bonito_pciconf_readl "HWADDR_FMT_plx"\n", addr);
354c0907c9eSPaolo Bonzini     return d->config_read(d, addr, 4);
355c0907c9eSPaolo Bonzini }
356c0907c9eSPaolo Bonzini 
357c0907c9eSPaolo Bonzini /* north bridge PCI configure space. 0x1fe0 0000 - 0x1fe0 00ff */
358c0907c9eSPaolo Bonzini 
359c0907c9eSPaolo Bonzini static const MemoryRegionOps bonito_pciconf_ops = {
360c0907c9eSPaolo Bonzini     .read = bonito_pciconf_readl,
361c0907c9eSPaolo Bonzini     .write = bonito_pciconf_writel,
362c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
363c0907c9eSPaolo Bonzini     .valid = {
364c0907c9eSPaolo Bonzini         .min_access_size = 4,
365c0907c9eSPaolo Bonzini         .max_access_size = 4,
366c0907c9eSPaolo Bonzini     },
367c0907c9eSPaolo Bonzini };
368c0907c9eSPaolo Bonzini 
bonito_ldma_readl(void * opaque,hwaddr addr,unsigned size)369c0907c9eSPaolo Bonzini static uint64_t bonito_ldma_readl(void *opaque, hwaddr addr,
370c0907c9eSPaolo Bonzini                                   unsigned size)
371c0907c9eSPaolo Bonzini {
372c0907c9eSPaolo Bonzini     uint32_t val;
373c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
374c0907c9eSPaolo Bonzini 
37558d47978SPeter Maydell     if (addr >= sizeof(s->bonldma)) {
37658d47978SPeter Maydell         return 0;
37758d47978SPeter Maydell     }
37858d47978SPeter Maydell 
379c0907c9eSPaolo Bonzini     val = ((uint32_t *)(&s->bonldma))[addr / sizeof(uint32_t)];
380c0907c9eSPaolo Bonzini 
381c0907c9eSPaolo Bonzini     return val;
382c0907c9eSPaolo Bonzini }
383c0907c9eSPaolo Bonzini 
bonito_ldma_writel(void * opaque,hwaddr addr,uint64_t val,unsigned size)384c0907c9eSPaolo Bonzini static void bonito_ldma_writel(void *opaque, hwaddr addr,
385c0907c9eSPaolo Bonzini                                uint64_t val, unsigned size)
386c0907c9eSPaolo Bonzini {
387c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
388c0907c9eSPaolo Bonzini 
38958d47978SPeter Maydell     if (addr >= sizeof(s->bonldma)) {
39058d47978SPeter Maydell         return;
39158d47978SPeter Maydell     }
39258d47978SPeter Maydell 
393c0907c9eSPaolo Bonzini     ((uint32_t *)(&s->bonldma))[addr / sizeof(uint32_t)] = val & 0xffffffff;
394c0907c9eSPaolo Bonzini }
395c0907c9eSPaolo Bonzini 
396c0907c9eSPaolo Bonzini static const MemoryRegionOps bonito_ldma_ops = {
397c0907c9eSPaolo Bonzini     .read = bonito_ldma_readl,
398c0907c9eSPaolo Bonzini     .write = bonito_ldma_writel,
399c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
400c0907c9eSPaolo Bonzini     .valid = {
401c0907c9eSPaolo Bonzini         .min_access_size = 4,
402c0907c9eSPaolo Bonzini         .max_access_size = 4,
403c0907c9eSPaolo Bonzini     },
404c0907c9eSPaolo Bonzini };
405c0907c9eSPaolo Bonzini 
bonito_cop_readl(void * opaque,hwaddr addr,unsigned size)406c0907c9eSPaolo Bonzini static uint64_t bonito_cop_readl(void *opaque, hwaddr addr,
407c0907c9eSPaolo Bonzini                                  unsigned size)
408c0907c9eSPaolo Bonzini {
409c0907c9eSPaolo Bonzini     uint32_t val;
410c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
411c0907c9eSPaolo Bonzini 
41258d47978SPeter Maydell     if (addr >= sizeof(s->boncop)) {
41358d47978SPeter Maydell         return 0;
41458d47978SPeter Maydell     }
41558d47978SPeter Maydell 
416c0907c9eSPaolo Bonzini     val = ((uint32_t *)(&s->boncop))[addr / sizeof(uint32_t)];
417c0907c9eSPaolo Bonzini 
418c0907c9eSPaolo Bonzini     return val;
419c0907c9eSPaolo Bonzini }
420c0907c9eSPaolo Bonzini 
bonito_cop_writel(void * opaque,hwaddr addr,uint64_t val,unsigned size)421c0907c9eSPaolo Bonzini static void bonito_cop_writel(void *opaque, hwaddr addr,
422c0907c9eSPaolo Bonzini                               uint64_t val, unsigned size)
423c0907c9eSPaolo Bonzini {
424c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
425c0907c9eSPaolo Bonzini 
42658d47978SPeter Maydell     if (addr >= sizeof(s->boncop)) {
42758d47978SPeter Maydell         return;
42858d47978SPeter Maydell     }
42958d47978SPeter Maydell 
430c0907c9eSPaolo Bonzini     ((uint32_t *)(&s->boncop))[addr / sizeof(uint32_t)] = val & 0xffffffff;
431c0907c9eSPaolo Bonzini }
432c0907c9eSPaolo Bonzini 
433c0907c9eSPaolo Bonzini static const MemoryRegionOps bonito_cop_ops = {
434c0907c9eSPaolo Bonzini     .read = bonito_cop_readl,
435c0907c9eSPaolo Bonzini     .write = bonito_cop_writel,
436c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
437c0907c9eSPaolo Bonzini     .valid = {
438c0907c9eSPaolo Bonzini         .min_access_size = 4,
439c0907c9eSPaolo Bonzini         .max_access_size = 4,
440c0907c9eSPaolo Bonzini     },
441c0907c9eSPaolo Bonzini };
442c0907c9eSPaolo Bonzini 
bonito_sbridge_pciaddr(void * opaque,hwaddr addr)443c0907c9eSPaolo Bonzini static uint32_t bonito_sbridge_pciaddr(void *opaque, hwaddr addr)
444c0907c9eSPaolo Bonzini {
445c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
446c0907c9eSPaolo Bonzini     PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
447c0907c9eSPaolo Bonzini     uint32_t cfgaddr;
448c0907c9eSPaolo Bonzini     uint32_t idsel;
449c0907c9eSPaolo Bonzini     uint32_t devno;
450c0907c9eSPaolo Bonzini     uint32_t funno;
451c0907c9eSPaolo Bonzini     uint32_t regno;
452c0907c9eSPaolo Bonzini     uint32_t pciaddr;
453c0907c9eSPaolo Bonzini 
454c0907c9eSPaolo Bonzini     /* support type0 pci config */
455c0907c9eSPaolo Bonzini     if ((s->regs[BONITO_PCIMAP_CFG] & 0x10000) != 0x0) {
456c0907c9eSPaolo Bonzini         return 0xffffffff;
457c0907c9eSPaolo Bonzini     }
458c0907c9eSPaolo Bonzini 
459c0907c9eSPaolo Bonzini     cfgaddr = addr & 0xffff;
460c0907c9eSPaolo Bonzini     cfgaddr |= (s->regs[BONITO_PCIMAP_CFG] & 0xffff) << 16;
461c0907c9eSPaolo Bonzini 
462f3db354cSFilip Bozuta     idsel = (cfgaddr & BONITO_PCICONF_IDSEL_MASK) >>
463f3db354cSFilip Bozuta              BONITO_PCICONF_IDSEL_OFFSET;
464786a4ea8SStefan Hajnoczi     devno = ctz32(idsel);
465c0907c9eSPaolo Bonzini     funno = (cfgaddr & BONITO_PCICONF_FUN_MASK) >> BONITO_PCICONF_FUN_OFFSET;
466711ef337SPhilippe Mathieu-Daudé     regno = (cfgaddr & BONITO_PCICONF_REG_MASK_HW) >> BONITO_PCICONF_REG_OFFSET;
467c0907c9eSPaolo Bonzini 
468c0907c9eSPaolo Bonzini     if (idsel == 0) {
469883f2c59SPhilippe Mathieu-Daudé         error_report("error in bonito pci config address 0x" HWADDR_FMT_plx
470ce3f3d30SPhilippe Mathieu-Daudé                      ",pcimap_cfg=0x%x", addr, s->regs[BONITO_PCIMAP_CFG]);
471c0907c9eSPaolo Bonzini         exit(1);
472c0907c9eSPaolo Bonzini     }
473c0907c9eSPaolo Bonzini     pciaddr = PCI_ADDR(pci_bus_num(phb->bus), devno, funno, regno);
474c0907c9eSPaolo Bonzini     DPRINTF("cfgaddr %x pciaddr %x busno %x devno %d funno %d regno %d\n",
475c0907c9eSPaolo Bonzini         cfgaddr, pciaddr, pci_bus_num(phb->bus), devno, funno, regno);
476c0907c9eSPaolo Bonzini 
477c0907c9eSPaolo Bonzini     return pciaddr;
478c0907c9eSPaolo Bonzini }
479c0907c9eSPaolo Bonzini 
bonito_spciconf_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)480421ab725SPeter Maydell static void bonito_spciconf_write(void *opaque, hwaddr addr, uint64_t val,
481421ab725SPeter Maydell                                   unsigned size)
482c0907c9eSPaolo Bonzini {
483c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
484c0907c9eSPaolo Bonzini     PCIDevice *d = PCI_DEVICE(s);
485c0907c9eSPaolo Bonzini     PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
486c0907c9eSPaolo Bonzini     uint32_t pciaddr;
487c0907c9eSPaolo Bonzini     uint16_t status;
488c0907c9eSPaolo Bonzini 
489883f2c59SPhilippe Mathieu-Daudé     DPRINTF("bonito_spciconf_write "HWADDR_FMT_plx" size %d val %lx\n",
490421ab725SPeter Maydell             addr, size, val);
491c0907c9eSPaolo Bonzini 
492c0907c9eSPaolo Bonzini     pciaddr = bonito_sbridge_pciaddr(s, addr);
493c0907c9eSPaolo Bonzini 
494c0907c9eSPaolo Bonzini     if (pciaddr == 0xffffffff) {
495c0907c9eSPaolo Bonzini         return;
496c0907c9eSPaolo Bonzini     }
497300491f9SPhilippe Mathieu-Daudé     if (addr & ~BONITO_PCICONF_REG_MASK_DS) {
498300491f9SPhilippe Mathieu-Daudé         trace_bonito_spciconf_small_access(addr, size);
499300491f9SPhilippe Mathieu-Daudé     }
500c0907c9eSPaolo Bonzini 
501c0907c9eSPaolo Bonzini     /* set the pci address in s->config_reg */
502c0907c9eSPaolo Bonzini     phb->config_reg = (pciaddr) | (1u << 31);
503421ab725SPeter Maydell     pci_data_write(phb->bus, phb->config_reg, val, size);
504c0907c9eSPaolo Bonzini 
505c0907c9eSPaolo Bonzini     /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
506c0907c9eSPaolo Bonzini     status = pci_get_word(d->config + PCI_STATUS);
507c0907c9eSPaolo Bonzini     status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
508c0907c9eSPaolo Bonzini     pci_set_word(d->config + PCI_STATUS, status);
509c0907c9eSPaolo Bonzini }
510c0907c9eSPaolo Bonzini 
bonito_spciconf_read(void * opaque,hwaddr addr,unsigned size)511421ab725SPeter Maydell static uint64_t bonito_spciconf_read(void *opaque, hwaddr addr, unsigned size)
512c0907c9eSPaolo Bonzini {
513c0907c9eSPaolo Bonzini     PCIBonitoState *s = opaque;
514c0907c9eSPaolo Bonzini     PCIDevice *d = PCI_DEVICE(s);
515c0907c9eSPaolo Bonzini     PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
516c0907c9eSPaolo Bonzini     uint32_t pciaddr;
517c0907c9eSPaolo Bonzini     uint16_t status;
518c0907c9eSPaolo Bonzini 
519883f2c59SPhilippe Mathieu-Daudé     DPRINTF("bonito_spciconf_read "HWADDR_FMT_plx" size %d\n", addr, size);
520c0907c9eSPaolo Bonzini 
521c0907c9eSPaolo Bonzini     pciaddr = bonito_sbridge_pciaddr(s, addr);
522c0907c9eSPaolo Bonzini 
523c0907c9eSPaolo Bonzini     if (pciaddr == 0xffffffff) {
524421ab725SPeter Maydell         return MAKE_64BIT_MASK(0, size * 8);
525c0907c9eSPaolo Bonzini     }
526300491f9SPhilippe Mathieu-Daudé     if (addr & ~BONITO_PCICONF_REG_MASK_DS) {
527300491f9SPhilippe Mathieu-Daudé         trace_bonito_spciconf_small_access(addr, size);
528300491f9SPhilippe Mathieu-Daudé     }
529c0907c9eSPaolo Bonzini 
530c0907c9eSPaolo Bonzini     /* set the pci address in s->config_reg */
531c0907c9eSPaolo Bonzini     phb->config_reg = (pciaddr) | (1u << 31);
532c0907c9eSPaolo Bonzini 
533c0907c9eSPaolo Bonzini     /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
534c0907c9eSPaolo Bonzini     status = pci_get_word(d->config + PCI_STATUS);
535c0907c9eSPaolo Bonzini     status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
536c0907c9eSPaolo Bonzini     pci_set_word(d->config + PCI_STATUS, status);
537c0907c9eSPaolo Bonzini 
538421ab725SPeter Maydell     return pci_data_read(phb->bus, phb->config_reg, size);
539c0907c9eSPaolo Bonzini }
540c0907c9eSPaolo Bonzini 
541c0907c9eSPaolo Bonzini /* south bridge PCI configure space. 0x1fe8 0000 - 0x1fef ffff */
542c0907c9eSPaolo Bonzini static const MemoryRegionOps bonito_spciconf_ops = {
543421ab725SPeter Maydell     .read = bonito_spciconf_read,
544421ab725SPeter Maydell     .write = bonito_spciconf_write,
545421ab725SPeter Maydell     .valid.min_access_size = 1,
546421ab725SPeter Maydell     .valid.max_access_size = 4,
547421ab725SPeter Maydell     .impl.min_access_size = 1,
548421ab725SPeter Maydell     .impl.max_access_size = 4,
549c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
550c0907c9eSPaolo Bonzini };
551c0907c9eSPaolo Bonzini 
552c0907c9eSPaolo Bonzini #define BONITO_IRQ_BASE 32
553c0907c9eSPaolo Bonzini 
pci_bonito_set_irq(void * opaque,int irq_num,int level)554c0907c9eSPaolo Bonzini static void pci_bonito_set_irq(void *opaque, int irq_num, int level)
555c0907c9eSPaolo Bonzini {
556c0907c9eSPaolo Bonzini     BonitoState *s = opaque;
557c0907c9eSPaolo Bonzini     qemu_irq *pic = s->pic;
558c0907c9eSPaolo Bonzini     PCIBonitoState *bonito_state = s->pci_dev;
559c0907c9eSPaolo Bonzini     int internal_irq = irq_num - BONITO_IRQ_BASE;
560c0907c9eSPaolo Bonzini 
561c0907c9eSPaolo Bonzini     if (bonito_state->regs[BONITO_INTEDGE] & (1 << internal_irq)) {
562c0907c9eSPaolo Bonzini         qemu_irq_pulse(*pic);
563c0907c9eSPaolo Bonzini     } else {   /* level triggered */
564c0907c9eSPaolo Bonzini         if (bonito_state->regs[BONITO_INTPOL] & (1 << internal_irq)) {
565c0907c9eSPaolo Bonzini             qemu_irq_raise(*pic);
566c0907c9eSPaolo Bonzini         } else {
567c0907c9eSPaolo Bonzini             qemu_irq_lower(*pic);
568c0907c9eSPaolo Bonzini         }
569c0907c9eSPaolo Bonzini     }
570c0907c9eSPaolo Bonzini }
571c0907c9eSPaolo Bonzini 
572c0907c9eSPaolo Bonzini /* map the original irq (0~3) to bonito irq (16~47, but 16~31 are unused) */
pci_bonito_map_irq(PCIDevice * pci_dev,int irq_num)573c0907c9eSPaolo Bonzini static int pci_bonito_map_irq(PCIDevice *pci_dev, int irq_num)
574c0907c9eSPaolo Bonzini {
575c0907c9eSPaolo Bonzini     int slot;
576c0907c9eSPaolo Bonzini 
5778d40def6SPhilippe Mathieu-Daudé     slot = PCI_SLOT(pci_dev->devfn);
578c0907c9eSPaolo Bonzini 
579c0907c9eSPaolo Bonzini     switch (slot) {
580c3a09ff6SPhilippe Mathieu-Daudé     case 5:   /* FULOONG2E_VIA_SLOT, SouthBridge, IDE, USB, ACPI, AC97, MC97 */
581c0907c9eSPaolo Bonzini         return irq_num % 4 + BONITO_IRQ_BASE;
582c3a09ff6SPhilippe Mathieu-Daudé     case 6:   /* FULOONG2E_ATI_SLOT, VGA */
583c0907c9eSPaolo Bonzini         return 4 + BONITO_IRQ_BASE;
584c3a09ff6SPhilippe Mathieu-Daudé     case 7:   /* FULOONG2E_RTL_SLOT, RTL8139 */
585c0907c9eSPaolo Bonzini         return 5 + BONITO_IRQ_BASE;
586c0907c9eSPaolo Bonzini     case 8 ... 12: /* PCI slot 1 to 4 */
587c0907c9eSPaolo Bonzini         return (slot - 8 + irq_num) + 6 + BONITO_IRQ_BASE;
588c0907c9eSPaolo Bonzini     default:  /* Unknown device, don't do any translation */
589c0907c9eSPaolo Bonzini         return irq_num;
590c0907c9eSPaolo Bonzini     }
591c0907c9eSPaolo Bonzini }
592c0907c9eSPaolo Bonzini 
bonito_reset_hold(Object * obj,ResetType type)593*ad80e367SPeter Maydell static void bonito_reset_hold(Object *obj, ResetType type)
594c0907c9eSPaolo Bonzini {
5954dd5cb5dSPhilippe Mathieu-Daudé     PCIBonitoState *s = PCI_BONITO(obj);
5961f8a6c8bSPhilippe Mathieu-Daudé     uint32_t val = 0;
597c0907c9eSPaolo Bonzini 
598c0907c9eSPaolo Bonzini     /* set the default value of north bridge registers */
599c0907c9eSPaolo Bonzini 
600c0907c9eSPaolo Bonzini     s->regs[BONITO_BONPONCFG] = 0xc40;
6011f8a6c8bSPhilippe Mathieu-Daudé     val = FIELD_DP32(val, BONGENCFG, PCIQUEUE, 1);
6021f8a6c8bSPhilippe Mathieu-Daudé     val = FIELD_DP32(val, BONGENCFG, WRITEBEHIND, 1);
6031f8a6c8bSPhilippe Mathieu-Daudé     val = FIELD_DP32(val, BONGENCFG, PREFETCH, 1);
6041f8a6c8bSPhilippe Mathieu-Daudé     val = FIELD_DP32(val, BONGENCFG, UNCACHED, 1);
6051f8a6c8bSPhilippe Mathieu-Daudé     val = FIELD_DP32(val, BONGENCFG, CPUSELFRESET, 1);
6061f8a6c8bSPhilippe Mathieu-Daudé     s->regs[BONITO_BONGENCFG] = val;
6071f8a6c8bSPhilippe Mathieu-Daudé 
608c0907c9eSPaolo Bonzini     s->regs[BONITO_IODEVCFG] = 0x2bff8010;
609c0907c9eSPaolo Bonzini     s->regs[BONITO_SDCFG] = 0x255e0091;
610c0907c9eSPaolo Bonzini 
611c0907c9eSPaolo Bonzini     s->regs[BONITO_GPIODATA] = 0x1ff;
612c0907c9eSPaolo Bonzini     s->regs[BONITO_GPIOIE] = 0x1ff;
613c0907c9eSPaolo Bonzini     s->regs[BONITO_DQCFG] = 0x8;
614c0907c9eSPaolo Bonzini     s->regs[BONITO_MEMSIZE] = 0x10000000;
615c0907c9eSPaolo Bonzini     s->regs[BONITO_PCIMAP] = 0x6140;
616c0907c9eSPaolo Bonzini }
617c0907c9eSPaolo Bonzini 
618c0907c9eSPaolo Bonzini static const VMStateDescription vmstate_bonito = {
619c0907c9eSPaolo Bonzini     .name = "Bonito",
620c0907c9eSPaolo Bonzini     .version_id = 1,
621c0907c9eSPaolo Bonzini     .minimum_version_id = 1,
622e2bd53a3SRichard Henderson     .fields = (const VMStateField[]) {
623c0907c9eSPaolo Bonzini         VMSTATE_PCI_DEVICE(dev, PCIBonitoState),
624c0907c9eSPaolo Bonzini         VMSTATE_END_OF_LIST()
625c0907c9eSPaolo Bonzini     }
626c0907c9eSPaolo Bonzini };
627c0907c9eSPaolo Bonzini 
bonito_host_realize(DeviceState * dev,Error ** errp)628f9ab9c6eSPhilippe Mathieu-Daudé static void bonito_host_realize(DeviceState *dev, Error **errp)
629c0907c9eSPaolo Bonzini {
630c0907c9eSPaolo Bonzini     PCIHostState *phb = PCI_HOST_BRIDGE(dev);
631f7cf2219SBALATON Zoltan     BonitoState *bs = BONITO_PCI_HOST_BRIDGE(dev);
632a0b544c1SPhilippe Mathieu-Daudé     MemoryRegion *pcimem_lo_alias = g_new(MemoryRegion, 3);
633c0907c9eSPaolo Bonzini 
634a0b544c1SPhilippe Mathieu-Daudé     memory_region_init(&bs->pci_mem, OBJECT(dev), "pci.mem", BONITO_PCIHI_SIZE);
6358e5c952bSPhilippe Mathieu-Daudé     phb->bus = pci_register_root_bus(dev, "pci",
6361115ff6dSDavid Gibson                                      pci_bonito_set_irq, pci_bonito_map_irq,
637f7cf2219SBALATON Zoltan                                      dev, &bs->pci_mem, get_system_io(),
6384934e479SPhilippe Mathieu-Daudé                                      PCI_DEVFN(5, 0), 32, TYPE_PCI_BUS);
639a0b544c1SPhilippe Mathieu-Daudé 
640a0b544c1SPhilippe Mathieu-Daudé     for (size_t i = 0; i < 3; i++) {
641a0b544c1SPhilippe Mathieu-Daudé         char *name = g_strdup_printf("pci.lomem%zu", i);
642a0b544c1SPhilippe Mathieu-Daudé 
643a0b544c1SPhilippe Mathieu-Daudé         memory_region_init_alias(&pcimem_lo_alias[i], NULL, name,
644a0b544c1SPhilippe Mathieu-Daudé                                  &bs->pci_mem, i * 64 * MiB, 64 * MiB);
645a0b544c1SPhilippe Mathieu-Daudé         memory_region_add_subregion(get_system_memory(),
646a0b544c1SPhilippe Mathieu-Daudé                                     BONITO_PCILO_BASE + i * 64 * MiB,
647a0b544c1SPhilippe Mathieu-Daudé                                     &pcimem_lo_alias[i]);
648a0b544c1SPhilippe Mathieu-Daudé         g_free(name);
649a0b544c1SPhilippe Mathieu-Daudé     }
650a0b544c1SPhilippe Mathieu-Daudé 
651a0b544c1SPhilippe Mathieu-Daudé     create_unimplemented_device("pci.io", BONITO_PCIIO_BASE, 1 * MiB);
652c0907c9eSPaolo Bonzini }
653c0907c9eSPaolo Bonzini 
bonito_pci_realize(PCIDevice * dev,Error ** errp)654eb66dac4SPhilippe Mathieu-Daudé static void bonito_pci_realize(PCIDevice *dev, Error **errp)
655c0907c9eSPaolo Bonzini {
656a2a645d9SCao jin     PCIBonitoState *s = PCI_BONITO(dev);
6570493aafbSPhilippe Mathieu-Daudé     MemoryRegion *host_mem = get_system_memory();
658c0907c9eSPaolo Bonzini     PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
6597d5b0d68SPhilippe Mathieu-Daudé     BonitoState *bs = s->pcihost;
660a0b544c1SPhilippe Mathieu-Daudé     MemoryRegion *pcimem_alias = g_new(MemoryRegion, 1);
661c0907c9eSPaolo Bonzini 
662f3db354cSFilip Bozuta     /*
663f3db354cSFilip Bozuta      * Bonito North Bridge, built on FPGA,
664f3db354cSFilip Bozuta      * VENDOR_ID/DEVICE_ID are "undefined"
665f3db354cSFilip Bozuta      */
666c0907c9eSPaolo Bonzini     pci_config_set_prog_interface(dev->config, 0x00);
667c0907c9eSPaolo Bonzini 
668c0907c9eSPaolo Bonzini     /* set the north bridge register mapping */
66940c5dce9SPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &bonito_ops, s,
670c0907c9eSPaolo Bonzini                           "north-bridge-register", BONITO_INTERNAL_REG_SIZE);
6710493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, BONITO_INTERNAL_REG_BASE, &s->iomem);
672c0907c9eSPaolo Bonzini 
673c0907c9eSPaolo Bonzini     /* set the north bridge pci configure  mapping */
67440c5dce9SPaolo Bonzini     memory_region_init_io(&phb->conf_mem, OBJECT(s), &bonito_pciconf_ops, s,
675c0907c9eSPaolo Bonzini                           "north-bridge-pci-config", BONITO_PCICONFIG_SIZE);
6760493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, BONITO_PCICONFIG_BASE,
6770493aafbSPhilippe Mathieu-Daudé                                 &phb->conf_mem);
678c0907c9eSPaolo Bonzini 
679c0907c9eSPaolo Bonzini     /* set the south bridge pci configure  mapping */
68040c5dce9SPaolo Bonzini     memory_region_init_io(&phb->data_mem, OBJECT(s), &bonito_spciconf_ops, s,
681c0907c9eSPaolo Bonzini                           "south-bridge-pci-config", BONITO_SPCICONFIG_SIZE);
6820493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, BONITO_SPCICONFIG_BASE,
6830493aafbSPhilippe Mathieu-Daudé                                 &phb->data_mem);
684c0907c9eSPaolo Bonzini 
68525cca0a9SPhilippe Mathieu-Daudé     create_unimplemented_device("bonito", BONITO_REG_BASE, BONITO_REG_SIZE);
68625cca0a9SPhilippe Mathieu-Daudé 
68740c5dce9SPaolo Bonzini     memory_region_init_io(&s->iomem_ldma, OBJECT(s), &bonito_ldma_ops, s,
688c0907c9eSPaolo Bonzini                           "ldma", 0x100);
6890493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, 0x1fe00200, &s->iomem_ldma);
690c0907c9eSPaolo Bonzini 
691a0b544c1SPhilippe Mathieu-Daudé     /* PCI copier */
69240c5dce9SPaolo Bonzini     memory_region_init_io(&s->iomem_cop, OBJECT(s), &bonito_cop_ops, s,
693c0907c9eSPaolo Bonzini                           "cop", 0x100);
6940493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, 0x1fe00300, &s->iomem_cop);
695c0907c9eSPaolo Bonzini 
6967a296990SPhilippe Mathieu-Daudé     create_unimplemented_device("ROMCS", BONITO_FLASH_BASE, 60 * MiB);
6977a296990SPhilippe Mathieu-Daudé 
698c0907c9eSPaolo Bonzini     /* Map PCI IO Space  0x1fd0 0000 - 0x1fd1 0000 */
699e37b80faSPaolo Bonzini     memory_region_init_alias(&s->bonito_pciio, OBJECT(s), "isa_mmio",
700e37b80faSPaolo Bonzini                              get_system_io(), 0, BONITO_PCIIO_SIZE);
7010493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, BONITO_PCIIO_BASE,
7020493aafbSPhilippe Mathieu-Daudé                                 &s->bonito_pciio);
703c0907c9eSPaolo Bonzini 
704c0907c9eSPaolo Bonzini     /* add pci local io mapping */
7057a296990SPhilippe Mathieu-Daudé 
7067a296990SPhilippe Mathieu-Daudé     memory_region_init_alias(&s->bonito_localio, OBJECT(s), "IOCS[0]",
7077a296990SPhilippe Mathieu-Daudé                              get_system_io(), 0, 256 * KiB);
7080493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, BONITO_DEV_BASE,
7090493aafbSPhilippe Mathieu-Daudé                                 &s->bonito_localio);
7107a296990SPhilippe Mathieu-Daudé     create_unimplemented_device("IOCS[1]", BONITO_DEV_BASE + 1 * 256 * KiB,
7117a296990SPhilippe Mathieu-Daudé                                 256 * KiB);
7127a296990SPhilippe Mathieu-Daudé     create_unimplemented_device("IOCS[2]", BONITO_DEV_BASE + 2 * 256 * KiB,
7137a296990SPhilippe Mathieu-Daudé                                 256 * KiB);
7147a296990SPhilippe Mathieu-Daudé     create_unimplemented_device("IOCS[3]", BONITO_DEV_BASE + 3 * 256 * KiB,
7157a296990SPhilippe Mathieu-Daudé                                 256 * KiB);
716c0907c9eSPaolo Bonzini 
717a0b544c1SPhilippe Mathieu-Daudé     memory_region_init_alias(pcimem_alias, NULL, "pci.mem.alias",
718a0b544c1SPhilippe Mathieu-Daudé                              &bs->pci_mem, 0, BONITO_PCIHI_SIZE);
7190493aafbSPhilippe Mathieu-Daudé     memory_region_add_subregion(host_mem, BONITO_PCIHI_BASE, pcimem_alias);
720a0b544c1SPhilippe Mathieu-Daudé     create_unimplemented_device("PCI_2",
721a0b544c1SPhilippe Mathieu-Daudé                                 (hwaddr)BONITO_PCIHI_BASE + BONITO_PCIHI_SIZE,
722a0b544c1SPhilippe Mathieu-Daudé                                 2 * GiB);
723a0b544c1SPhilippe Mathieu-Daudé 
724c0907c9eSPaolo Bonzini     /* set the default value of north bridge pci config */
725c0907c9eSPaolo Bonzini     pci_set_word(dev->config + PCI_COMMAND, 0x0000);
726c0907c9eSPaolo Bonzini     pci_set_word(dev->config + PCI_STATUS, 0x0000);
727c0907c9eSPaolo Bonzini     pci_set_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID, 0x0000);
728c0907c9eSPaolo Bonzini     pci_set_word(dev->config + PCI_SUBSYSTEM_ID, 0x0000);
729c0907c9eSPaolo Bonzini 
730c0907c9eSPaolo Bonzini     pci_set_byte(dev->config + PCI_INTERRUPT_LINE, 0x00);
731b4bb339bSPhilippe Mathieu-Daudé     pci_config_set_interrupt_pin(dev->config, 0x01); /* interrupt pin A */
732b4bb339bSPhilippe Mathieu-Daudé 
733c0907c9eSPaolo Bonzini     pci_set_byte(dev->config + PCI_MIN_GNT, 0x3c);
734c0907c9eSPaolo Bonzini     pci_set_byte(dev->config + PCI_MAX_LAT, 0x00);
735c0907c9eSPaolo Bonzini }
736c0907c9eSPaolo Bonzini 
bonito_init(qemu_irq * pic)737c0907c9eSPaolo Bonzini PCIBus *bonito_init(qemu_irq *pic)
738c0907c9eSPaolo Bonzini {
739c0907c9eSPaolo Bonzini     DeviceState *dev;
740c0907c9eSPaolo Bonzini     BonitoState *pcihost;
741c0907c9eSPaolo Bonzini     PCIHostState *phb;
742c0907c9eSPaolo Bonzini     PCIBonitoState *s;
743c0907c9eSPaolo Bonzini     PCIDevice *d;
744c0907c9eSPaolo Bonzini 
7453e80f690SMarkus Armbruster     dev = qdev_new(TYPE_BONITO_PCI_HOST_BRIDGE);
746c0907c9eSPaolo Bonzini     phb = PCI_HOST_BRIDGE(dev);
747c0907c9eSPaolo Bonzini     pcihost = BONITO_PCI_HOST_BRIDGE(dev);
748c0907c9eSPaolo Bonzini     pcihost->pic = pic;
7493c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
750c0907c9eSPaolo Bonzini 
7519307d06dSMarkus Armbruster     d = pci_new(PCI_DEVFN(0, 0), TYPE_PCI_BONITO);
752a2a645d9SCao jin     s = PCI_BONITO(d);
753c0907c9eSPaolo Bonzini     s->pcihost = pcihost;
754c0907c9eSPaolo Bonzini     pcihost->pci_dev = s;
7559307d06dSMarkus Armbruster     pci_realize_and_unref(d, phb->bus, &error_fatal);
756c0907c9eSPaolo Bonzini 
757c0907c9eSPaolo Bonzini     return phb->bus;
758c0907c9eSPaolo Bonzini }
759c0907c9eSPaolo Bonzini 
bonito_pci_class_init(ObjectClass * klass,void * data)760eb66dac4SPhilippe Mathieu-Daudé static void bonito_pci_class_init(ObjectClass *klass, void *data)
761c0907c9eSPaolo Bonzini {
762c0907c9eSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
763c0907c9eSPaolo Bonzini     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
7644dd5cb5dSPhilippe Mathieu-Daudé     ResettableClass *rc = RESETTABLE_CLASS(klass);
765c0907c9eSPaolo Bonzini 
7664dd5cb5dSPhilippe Mathieu-Daudé     rc->phases.hold = bonito_reset_hold;
767eb66dac4SPhilippe Mathieu-Daudé     k->realize = bonito_pci_realize;
768c0907c9eSPaolo Bonzini     k->vendor_id = 0xdf53;
769c0907c9eSPaolo Bonzini     k->device_id = 0x00d5;
770c0907c9eSPaolo Bonzini     k->revision = 0x01;
771c0907c9eSPaolo Bonzini     k->class_id = PCI_CLASS_BRIDGE_HOST;
772c0907c9eSPaolo Bonzini     dc->desc = "Host bridge";
773c0907c9eSPaolo Bonzini     dc->vmsd = &vmstate_bonito;
77408c58f92SMarkus Armbruster     /*
77508c58f92SMarkus Armbruster      * PCI-facing part of the host bridge, not usable without the
77608c58f92SMarkus Armbruster      * host-facing part, which can't be device_add'ed, yet.
77708c58f92SMarkus Armbruster      */
778e90f2a8cSEduardo Habkost     dc->user_creatable = false;
779c0907c9eSPaolo Bonzini }
780c0907c9eSPaolo Bonzini 
781eb66dac4SPhilippe Mathieu-Daudé static const TypeInfo bonito_pci_info = {
782a2a645d9SCao jin     .name          = TYPE_PCI_BONITO,
783c0907c9eSPaolo Bonzini     .parent        = TYPE_PCI_DEVICE,
784c0907c9eSPaolo Bonzini     .instance_size = sizeof(PCIBonitoState),
785eb66dac4SPhilippe Mathieu-Daudé     .class_init    = bonito_pci_class_init,
786fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
787fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
788fd3b02c8SEduardo Habkost         { },
789fd3b02c8SEduardo Habkost     },
790c0907c9eSPaolo Bonzini };
791c0907c9eSPaolo Bonzini 
bonito_host_class_init(ObjectClass * klass,void * data)792f9ab9c6eSPhilippe Mathieu-Daudé static void bonito_host_class_init(ObjectClass *klass, void *data)
793c0907c9eSPaolo Bonzini {
794e800894aSPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(klass);
795c0907c9eSPaolo Bonzini 
796f9ab9c6eSPhilippe Mathieu-Daudé     dc->realize = bonito_host_realize;
797c0907c9eSPaolo Bonzini }
798c0907c9eSPaolo Bonzini 
799f9ab9c6eSPhilippe Mathieu-Daudé static const TypeInfo bonito_host_info = {
800c0907c9eSPaolo Bonzini     .name          = TYPE_BONITO_PCI_HOST_BRIDGE,
801c0907c9eSPaolo Bonzini     .parent        = TYPE_PCI_HOST_BRIDGE,
802c0907c9eSPaolo Bonzini     .instance_size = sizeof(BonitoState),
803f9ab9c6eSPhilippe Mathieu-Daudé     .class_init    = bonito_host_class_init,
804c0907c9eSPaolo Bonzini };
805c0907c9eSPaolo Bonzini 
bonito_register_types(void)806c0907c9eSPaolo Bonzini static void bonito_register_types(void)
807c0907c9eSPaolo Bonzini {
808f9ab9c6eSPhilippe Mathieu-Daudé     type_register_static(&bonito_host_info);
809eb66dac4SPhilippe Mathieu-Daudé     type_register_static(&bonito_pci_info);
810c0907c9eSPaolo Bonzini }
811c0907c9eSPaolo Bonzini 
812c0907c9eSPaolo Bonzini type_init(bonito_register_types)
813