hw/ppc: change indentation to spaces from TABs
[qemu.git] / hw / isa / piix4.c
blob8607e0ac36adfa8cb6009893d433a50f9ae4d5b4
1 /*
2 * QEMU PIIX4 PCI Bridge Emulation
4 * Copyright (c) 2006 Fabrice Bellard
5 * Copyright (c) 2018 Hervé Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "hw/irq.h"
29 #include "hw/southbridge/piix.h"
30 #include "hw/pci/pci.h"
31 #include "hw/isa/isa.h"
32 #include "hw/intc/i8259.h"
33 #include "hw/dma/i8257.h"
34 #include "hw/timer/i8254.h"
35 #include "hw/rtc/mc146818rtc.h"
36 #include "hw/ide/pci.h"
37 #include "migration/vmstate.h"
38 #include "sysemu/reset.h"
39 #include "sysemu/runstate.h"
40 #include "qom/object.h"
42 struct PIIX4State {
43 PCIDevice dev;
44 qemu_irq cpu_intr;
45 qemu_irq *isa;
47 RTCState rtc;
48 /* Reset Control Register */
49 MemoryRegion rcr_mem;
50 uint8_t rcr;
53 OBJECT_DECLARE_SIMPLE_TYPE(PIIX4State, PIIX4_PCI_DEVICE)
55 static void piix4_set_irq(void *opaque, int irq_num, int level)
57 int i, pic_irq, pic_level;
58 PIIX4State *s = opaque;
59 PCIBus *bus = pci_get_bus(&s->dev);
61 /* now we change the pic irq level according to the piix irq mappings */
62 /* XXX: optimize */
63 pic_irq = s->dev.config[PIIX_PIRQCA + irq_num];
64 if (pic_irq < ISA_NUM_IRQS) {
65 /* The pic level is the logical OR of all the PCI irqs mapped to it. */
66 pic_level = 0;
67 for (i = 0; i < PIIX_NUM_PIRQS; i++) {
68 if (pic_irq == s->dev.config[PIIX_PIRQCA + i]) {
69 pic_level |= pci_bus_get_irq_level(bus, i);
72 qemu_set_irq(s->isa[pic_irq], pic_level);
76 static void piix4_isa_reset(DeviceState *dev)
78 PIIX4State *d = PIIX4_PCI_DEVICE(dev);
79 uint8_t *pci_conf = d->dev.config;
81 pci_conf[0x04] = 0x07; // master, memory and I/O
82 pci_conf[0x05] = 0x00;
83 pci_conf[0x06] = 0x00;
84 pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
85 pci_conf[0x4c] = 0x4d;
86 pci_conf[0x4e] = 0x03;
87 pci_conf[0x4f] = 0x00;
88 pci_conf[0x60] = 0x0a; // PCI A -> IRQ 10
89 pci_conf[0x61] = 0x0a; // PCI B -> IRQ 10
90 pci_conf[0x62] = 0x0b; // PCI C -> IRQ 11
91 pci_conf[0x63] = 0x0b; // PCI D -> IRQ 11
92 pci_conf[0x69] = 0x02;
93 pci_conf[0x70] = 0x80;
94 pci_conf[0x76] = 0x0c;
95 pci_conf[0x77] = 0x0c;
96 pci_conf[0x78] = 0x02;
97 pci_conf[0x79] = 0x00;
98 pci_conf[0x80] = 0x00;
99 pci_conf[0x82] = 0x00;
100 pci_conf[0xa0] = 0x08;
101 pci_conf[0xa2] = 0x00;
102 pci_conf[0xa3] = 0x00;
103 pci_conf[0xa4] = 0x00;
104 pci_conf[0xa5] = 0x00;
105 pci_conf[0xa6] = 0x00;
106 pci_conf[0xa7] = 0x00;
107 pci_conf[0xa8] = 0x0f;
108 pci_conf[0xaa] = 0x00;
109 pci_conf[0xab] = 0x00;
110 pci_conf[0xac] = 0x00;
111 pci_conf[0xae] = 0x00;
114 static int piix4_ide_post_load(void *opaque, int version_id)
116 PIIX4State *s = opaque;
118 if (version_id == 2) {
119 s->rcr = 0;
122 return 0;
125 static const VMStateDescription vmstate_piix4 = {
126 .name = "PIIX4",
127 .version_id = 3,
128 .minimum_version_id = 2,
129 .post_load = piix4_ide_post_load,
130 .fields = (VMStateField[]) {
131 VMSTATE_PCI_DEVICE(dev, PIIX4State),
132 VMSTATE_UINT8_V(rcr, PIIX4State, 3),
133 VMSTATE_END_OF_LIST()
137 static void piix4_request_i8259_irq(void *opaque, int irq, int level)
139 PIIX4State *s = opaque;
140 qemu_set_irq(s->cpu_intr, level);
143 static void piix4_set_i8259_irq(void *opaque, int irq, int level)
145 PIIX4State *s = opaque;
146 qemu_set_irq(s->isa[irq], level);
149 static void piix4_rcr_write(void *opaque, hwaddr addr, uint64_t val,
150 unsigned int len)
152 PIIX4State *s = opaque;
154 if (val & 4) {
155 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
156 return;
159 s->rcr = val & 2; /* keep System Reset type only */
162 static uint64_t piix4_rcr_read(void *opaque, hwaddr addr, unsigned int len)
164 PIIX4State *s = opaque;
166 return s->rcr;
169 static const MemoryRegionOps piix4_rcr_ops = {
170 .read = piix4_rcr_read,
171 .write = piix4_rcr_write,
172 .endianness = DEVICE_LITTLE_ENDIAN,
173 .impl = {
174 .min_access_size = 1,
175 .max_access_size = 1,
179 static void piix4_realize(PCIDevice *dev, Error **errp)
181 PIIX4State *s = PIIX4_PCI_DEVICE(dev);
182 ISABus *isa_bus;
183 qemu_irq *i8259_out_irq;
185 isa_bus = isa_bus_new(DEVICE(dev), pci_address_space(dev),
186 pci_address_space_io(dev), errp);
187 if (!isa_bus) {
188 return;
191 qdev_init_gpio_in_named(DEVICE(dev), piix4_set_i8259_irq,
192 "isa", ISA_NUM_IRQS);
193 qdev_init_gpio_out_named(DEVICE(dev), &s->cpu_intr,
194 "intr", 1);
196 memory_region_init_io(&s->rcr_mem, OBJECT(dev), &piix4_rcr_ops, s,
197 "reset-control", 1);
198 memory_region_add_subregion_overlap(pci_address_space_io(dev),
199 PIIX_RCR_IOPORT, &s->rcr_mem, 1);
201 /* initialize i8259 pic */
202 i8259_out_irq = qemu_allocate_irqs(piix4_request_i8259_irq, s, 1);
203 s->isa = i8259_init(isa_bus, *i8259_out_irq);
205 /* initialize ISA irqs */
206 isa_bus_irqs(isa_bus, s->isa);
208 /* initialize pit */
209 i8254_pit_init(isa_bus, 0x40, 0, NULL);
211 /* DMA */
212 i8257_dma_init(isa_bus, 0);
214 /* RTC */
215 qdev_prop_set_int32(DEVICE(&s->rtc), "base_year", 2000);
216 if (!qdev_realize(DEVICE(&s->rtc), BUS(isa_bus), errp)) {
217 return;
219 s->rtc.irq = isa_get_irq(ISA_DEVICE(&s->rtc), s->rtc.isairq);
222 static void piix4_init(Object *obj)
224 PIIX4State *s = PIIX4_PCI_DEVICE(obj);
226 object_initialize(&s->rtc, sizeof(s->rtc), TYPE_MC146818_RTC);
229 static void piix4_class_init(ObjectClass *klass, void *data)
231 DeviceClass *dc = DEVICE_CLASS(klass);
232 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
234 k->realize = piix4_realize;
235 k->vendor_id = PCI_VENDOR_ID_INTEL;
236 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_0;
237 k->class_id = PCI_CLASS_BRIDGE_ISA;
238 dc->reset = piix4_isa_reset;
239 dc->desc = "ISA bridge";
240 dc->vmsd = &vmstate_piix4;
242 * Reason: part of PIIX4 southbridge, needs to be wired up,
243 * e.g. by mips_malta_init()
245 dc->user_creatable = false;
246 dc->hotpluggable = false;
249 static const TypeInfo piix4_info = {
250 .name = TYPE_PIIX4_PCI_DEVICE,
251 .parent = TYPE_PCI_DEVICE,
252 .instance_size = sizeof(PIIX4State),
253 .instance_init = piix4_init,
254 .class_init = piix4_class_init,
255 .interfaces = (InterfaceInfo[]) {
256 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
257 { },
261 static void piix4_register_types(void)
263 type_register_static(&piix4_info);
266 type_init(piix4_register_types)
268 static int pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
270 int slot;
272 slot = PCI_SLOT(pci_dev->devfn);
274 switch (slot) {
275 /* PIIX4 USB */
276 case 10:
277 return 3;
278 /* AMD 79C973 Ethernet */
279 case 11:
280 return 1;
281 /* Crystal 4281 Sound */
282 case 12:
283 return 2;
284 /* PCI slot 1 to 4 */
285 case 18 ... 21:
286 return ((slot - 18) + irq_num) & 0x03;
287 /* Unknown device, don't do any translation */
288 default:
289 return irq_num;
293 DeviceState *piix4_create(PCIBus *pci_bus, ISABus **isa_bus, I2CBus **smbus)
295 PIIX4State *s;
296 PCIDevice *pci;
297 DeviceState *dev;
298 int devfn = PCI_DEVFN(10, 0);
300 pci = pci_create_simple_multifunction(pci_bus, devfn, true,
301 TYPE_PIIX4_PCI_DEVICE);
302 dev = DEVICE(pci);
303 s = PIIX4_PCI_DEVICE(pci);
304 if (isa_bus) {
305 *isa_bus = ISA_BUS(qdev_get_child_bus(dev, "isa.0"));
308 pci = pci_create_simple(pci_bus, devfn + 1, "piix4-ide");
309 pci_ide_create_devs(pci);
311 pci_create_simple(pci_bus, devfn + 2, "piix4-usb-uhci");
312 if (smbus) {
313 *smbus = piix4_pm_init(pci_bus, devfn + 3, 0x1100,
314 qdev_get_gpio_in_named(dev, "isa", 9),
315 NULL, 0, NULL);
318 pci_bus_irqs(pci_bus, piix4_set_irq, pci_slot_get_pirq, s, PIIX_NUM_PIRQS);
320 return dev;