xref: /qemu/hw/isa/piix.c (revision 2922dbc28c74a4b3976cb4bc020980030ccfef67)
1 /*
2  * QEMU PIIX PCI ISA Bridge Emulation
3  *
4  * Copyright (c) 2006 Fabrice Bellard
5  * Copyright (c) 2018 Hervé Poussineau
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  */
25 
26 #include "qemu/osdep.h"
27 #include "qemu/range.h"
28 #include "qapi/error.h"
29 #include "hw/dma/i8257.h"
30 #include "hw/southbridge/piix.h"
31 #include "hw/timer/i8254.h"
32 #include "hw/irq.h"
33 #include "hw/qdev-properties.h"
34 #include "hw/ide/piix.h"
35 #include "hw/intc/i8259.h"
36 #include "hw/isa/isa.h"
37 #include "sysemu/runstate.h"
38 #include "migration/vmstate.h"
39 #include "hw/acpi/acpi_aml_interface.h"
40 
41 static void piix3_set_irq_pic(PIIXState *piix3, int pic_irq)
42 {
43     qemu_set_irq(piix3->isa_irqs_in[pic_irq],
44                  !!(piix3->pic_levels &
45                     (((1ULL << PIIX_NUM_PIRQS) - 1) <<
46                      (pic_irq * PIIX_NUM_PIRQS))));
47 }
48 
49 static void piix3_set_irq_level_internal(PIIXState *piix3, int pirq, int level)
50 {
51     int pic_irq;
52     uint64_t mask;
53 
54     pic_irq = piix3->dev.config[PIIX_PIRQCA + pirq];
55     if (pic_irq >= ISA_NUM_IRQS) {
56         return;
57     }
58 
59     mask = 1ULL << ((pic_irq * PIIX_NUM_PIRQS) + pirq);
60     piix3->pic_levels &= ~mask;
61     piix3->pic_levels |= mask * !!level;
62 }
63 
64 static void piix3_set_irq_level(PIIXState *piix3, int pirq, int level)
65 {
66     int pic_irq;
67 
68     pic_irq = piix3->dev.config[PIIX_PIRQCA + pirq];
69     if (pic_irq >= ISA_NUM_IRQS) {
70         return;
71     }
72 
73     piix3_set_irq_level_internal(piix3, pirq, level);
74 
75     piix3_set_irq_pic(piix3, pic_irq);
76 }
77 
78 static void piix3_set_irq(void *opaque, int pirq, int level)
79 {
80     PIIXState *piix3 = opaque;
81     piix3_set_irq_level(piix3, pirq, level);
82 }
83 
84 static void piix4_set_irq(void *opaque, int irq_num, int level)
85 {
86     int i, pic_irq, pic_level;
87     PIIXState *s = opaque;
88     PCIBus *bus = pci_get_bus(&s->dev);
89 
90     /* now we change the pic irq level according to the piix irq mappings */
91     /* XXX: optimize */
92     pic_irq = s->dev.config[PIIX_PIRQCA + irq_num];
93     if (pic_irq < ISA_NUM_IRQS) {
94         /* The pic level is the logical OR of all the PCI irqs mapped to it. */
95         pic_level = 0;
96         for (i = 0; i < PIIX_NUM_PIRQS; i++) {
97             if (pic_irq == s->dev.config[PIIX_PIRQCA + i]) {
98                 pic_level |= pci_bus_get_irq_level(bus, i);
99             }
100         }
101         qemu_set_irq(s->isa_irqs_in[pic_irq], pic_level);
102     }
103 }
104 
105 static void piix_request_i8259_irq(void *opaque, int irq, int level)
106 {
107     PIIXState *s = opaque;
108     qemu_set_irq(s->cpu_intr, level);
109 }
110 
111 static PCIINTxRoute piix3_route_intx_pin_to_irq(void *opaque, int pin)
112 {
113     PIIXState *piix3 = opaque;
114     int irq = piix3->dev.config[PIIX_PIRQCA + pin];
115     PCIINTxRoute route;
116 
117     if (irq < ISA_NUM_IRQS) {
118         route.mode = PCI_INTX_ENABLED;
119         route.irq = irq;
120     } else {
121         route.mode = PCI_INTX_DISABLED;
122         route.irq = -1;
123     }
124     return route;
125 }
126 
127 /* irq routing is changed. so rebuild bitmap */
128 static void piix3_update_irq_levels(PIIXState *piix3)
129 {
130     PCIBus *bus = pci_get_bus(&piix3->dev);
131     int pirq;
132 
133     piix3->pic_levels = 0;
134     for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
135         piix3_set_irq_level(piix3, pirq, pci_bus_get_irq_level(bus, pirq));
136     }
137 }
138 
139 static void piix3_write_config(PCIDevice *dev,
140                                uint32_t address, uint32_t val, int len)
141 {
142     pci_default_write_config(dev, address, val, len);
143     if (ranges_overlap(address, len, PIIX_PIRQCA, 4)) {
144         PIIXState *piix3 = PIIX_PCI_DEVICE(dev);
145         int pic_irq;
146 
147         pci_bus_fire_intx_routing_notifier(pci_get_bus(&piix3->dev));
148         piix3_update_irq_levels(piix3);
149         for (pic_irq = 0; pic_irq < ISA_NUM_IRQS; pic_irq++) {
150             piix3_set_irq_pic(piix3, pic_irq);
151         }
152     }
153 }
154 
155 static void piix_reset(DeviceState *dev)
156 {
157     PIIXState *d = PIIX_PCI_DEVICE(dev);
158     uint8_t *pci_conf = d->dev.config;
159 
160     pci_conf[0x04] = 0x07; /* master, memory and I/O */
161     pci_conf[0x05] = 0x00;
162     pci_conf[0x06] = 0x00;
163     pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
164     pci_conf[0x4c] = 0x4d;
165     pci_conf[0x4e] = 0x03;
166     pci_conf[0x4f] = 0x00;
167     pci_conf[0x60] = 0x80;
168     pci_conf[0x61] = 0x80;
169     pci_conf[0x62] = 0x80;
170     pci_conf[0x63] = 0x80;
171     pci_conf[0x69] = 0x02;
172     pci_conf[0x70] = 0x80;
173     pci_conf[0x76] = 0x0c;
174     pci_conf[0x77] = 0x0c;
175     pci_conf[0x78] = 0x02;
176     pci_conf[0x79] = 0x00;
177     pci_conf[0x80] = 0x00;
178     pci_conf[0x82] = 0x00;
179     pci_conf[0xa0] = 0x08;
180     pci_conf[0xa2] = 0x00;
181     pci_conf[0xa3] = 0x00;
182     pci_conf[0xa4] = 0x00;
183     pci_conf[0xa5] = 0x00;
184     pci_conf[0xa6] = 0x00;
185     pci_conf[0xa7] = 0x00;
186     pci_conf[0xa8] = 0x0f;
187     pci_conf[0xaa] = 0x00;
188     pci_conf[0xab] = 0x00;
189     pci_conf[0xac] = 0x00;
190     pci_conf[0xae] = 0x00;
191 
192     d->pic_levels = 0;
193     d->rcr = 0;
194 }
195 
196 static int piix3_post_load(void *opaque, int version_id)
197 {
198     PIIXState *piix3 = opaque;
199     int pirq;
200 
201     /*
202      * Because the i8259 has not been deserialized yet, qemu_irq_raise
203      * might bring the system to a different state than the saved one;
204      * for example, the interrupt could be masked but the i8259 would
205      * not know that yet and would trigger an interrupt in the CPU.
206      *
207      * Here, we update irq levels without raising the interrupt.
208      * Interrupt state will be deserialized separately through the i8259.
209      */
210     piix3->pic_levels = 0;
211     for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
212         piix3_set_irq_level_internal(piix3, pirq,
213             pci_bus_get_irq_level(pci_get_bus(&piix3->dev), pirq));
214     }
215     return 0;
216 }
217 
218 static int piix4_post_load(void *opaque, int version_id)
219 {
220     PIIXState *s = opaque;
221 
222     if (version_id == 2) {
223         s->rcr = 0;
224     }
225 
226     return 0;
227 }
228 
229 static int piix3_pre_save(void *opaque)
230 {
231     int i;
232     PIIXState *piix3 = opaque;
233 
234     for (i = 0; i < ARRAY_SIZE(piix3->pci_irq_levels_vmstate); i++) {
235         piix3->pci_irq_levels_vmstate[i] =
236             pci_bus_get_irq_level(pci_get_bus(&piix3->dev), i);
237     }
238 
239     return 0;
240 }
241 
242 static bool piix3_rcr_needed(void *opaque)
243 {
244     PIIXState *piix3 = opaque;
245 
246     return (piix3->rcr != 0);
247 }
248 
249 static const VMStateDescription vmstate_piix3_rcr = {
250     .name = "PIIX3/rcr",
251     .version_id = 1,
252     .minimum_version_id = 1,
253     .needed = piix3_rcr_needed,
254     .fields = (VMStateField[]) {
255         VMSTATE_UINT8(rcr, PIIXState),
256         VMSTATE_END_OF_LIST()
257     }
258 };
259 
260 static const VMStateDescription vmstate_piix3 = {
261     .name = "PIIX3",
262     .version_id = 3,
263     .minimum_version_id = 2,
264     .post_load = piix3_post_load,
265     .pre_save = piix3_pre_save,
266     .fields = (VMStateField[]) {
267         VMSTATE_PCI_DEVICE(dev, PIIXState),
268         VMSTATE_INT32_ARRAY_V(pci_irq_levels_vmstate, PIIXState,
269                               PIIX_NUM_PIRQS, 3),
270         VMSTATE_END_OF_LIST()
271     },
272     .subsections = (const VMStateDescription*[]) {
273         &vmstate_piix3_rcr,
274         NULL
275     }
276 };
277 
278 static const VMStateDescription vmstate_piix4 = {
279     .name = "PIIX4",
280     .version_id = 3,
281     .minimum_version_id = 2,
282     .post_load = piix4_post_load,
283     .fields = (VMStateField[]) {
284         VMSTATE_PCI_DEVICE(dev, PIIXState),
285         VMSTATE_UINT8_V(rcr, PIIXState, 3),
286         VMSTATE_END_OF_LIST()
287     }
288 };
289 
290 static void rcr_write(void *opaque, hwaddr addr, uint64_t val, unsigned len)
291 {
292     PIIXState *d = opaque;
293 
294     if (val & 4) {
295         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
296         return;
297     }
298     d->rcr = val & 2; /* keep System Reset type only */
299 }
300 
301 static uint64_t rcr_read(void *opaque, hwaddr addr, unsigned len)
302 {
303     PIIXState *d = opaque;
304 
305     return d->rcr;
306 }
307 
308 static const MemoryRegionOps rcr_ops = {
309     .read = rcr_read,
310     .write = rcr_write,
311     .endianness = DEVICE_LITTLE_ENDIAN,
312     .impl = {
313         .min_access_size = 1,
314         .max_access_size = 1,
315     },
316 };
317 
318 static void pci_piix_realize(PCIDevice *dev, const char *uhci_type,
319                              Error **errp)
320 {
321     PIIXState *d = PIIX_PCI_DEVICE(dev);
322     PCIBus *pci_bus = pci_get_bus(dev);
323     ISABus *isa_bus;
324     uint32_t irq;
325 
326     isa_bus = isa_bus_new(DEVICE(d), pci_address_space(dev),
327                           pci_address_space_io(dev), errp);
328     if (!isa_bus) {
329         return;
330     }
331 
332     memory_region_init_io(&d->rcr_mem, OBJECT(dev), &rcr_ops, d,
333                           "piix-reset-control", 1);
334     memory_region_add_subregion_overlap(pci_address_space_io(dev),
335                                         PIIX_RCR_IOPORT, &d->rcr_mem, 1);
336 
337     /* PIC */
338     if (d->has_pic) {
339         qemu_irq *i8259_out_irq = qemu_allocate_irqs(piix_request_i8259_irq, d,
340                                                      1);
341         qemu_irq *i8259 = i8259_init(isa_bus, *i8259_out_irq);
342         size_t i;
343 
344         for (i = 0; i < ISA_NUM_IRQS; i++) {
345             d->isa_irqs_in[i] = i8259[i];
346         }
347 
348         g_free(i8259);
349 
350         qdev_init_gpio_out_named(DEVICE(dev), &d->cpu_intr, "intr", 1);
351     }
352 
353     isa_bus_register_input_irqs(isa_bus, d->isa_irqs_in);
354 
355     /* PIT */
356     if (d->has_pit) {
357         i8254_pit_init(isa_bus, 0x40, 0, NULL);
358     }
359 
360     i8257_dma_init(isa_bus, 0);
361 
362     /* RTC */
363     qdev_prop_set_int32(DEVICE(&d->rtc), "base_year", 2000);
364     if (!qdev_realize(DEVICE(&d->rtc), BUS(isa_bus), errp)) {
365         return;
366     }
367     irq = object_property_get_uint(OBJECT(&d->rtc), "irq", &error_fatal);
368     isa_connect_gpio_out(ISA_DEVICE(&d->rtc), 0, irq);
369 
370     /* IDE */
371     qdev_prop_set_int32(DEVICE(&d->ide), "addr", dev->devfn + 1);
372     if (!qdev_realize(DEVICE(&d->ide), BUS(pci_bus), errp)) {
373         return;
374     }
375 
376     /* USB */
377     if (d->has_usb) {
378         object_initialize_child(OBJECT(dev), "uhci", &d->uhci, uhci_type);
379         qdev_prop_set_int32(DEVICE(&d->uhci), "addr", dev->devfn + 2);
380         if (!qdev_realize(DEVICE(&d->uhci), BUS(pci_bus), errp)) {
381             return;
382         }
383     }
384 
385     /* Power Management */
386     if (d->has_acpi) {
387         object_initialize_child(OBJECT(d), "pm", &d->pm, TYPE_PIIX4_PM);
388         qdev_prop_set_int32(DEVICE(&d->pm), "addr", dev->devfn + 3);
389         qdev_prop_set_uint32(DEVICE(&d->pm), "smb_io_base", d->smb_io_base);
390         qdev_prop_set_bit(DEVICE(&d->pm), "smm-enabled", d->smm_enabled);
391         if (!qdev_realize(DEVICE(&d->pm), BUS(pci_bus), errp)) {
392             return;
393         }
394         qdev_connect_gpio_out(DEVICE(&d->pm), 0, d->isa_irqs_in[9]);
395     }
396 }
397 
398 static void build_pci_isa_aml(AcpiDevAmlIf *adev, Aml *scope)
399 {
400     Aml *field;
401     Aml *sb_scope = aml_scope("\\_SB");
402     BusState *bus = qdev_get_child_bus(DEVICE(adev), "isa.0");
403 
404     /* PIIX PCI to ISA irq remapping */
405     aml_append(scope, aml_operation_region("P40C", AML_PCI_CONFIG,
406                                            aml_int(0x60), 0x04));
407     /* Fields declarion has to happen *after* operation region */
408     field = aml_field("PCI0.S08.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
409     aml_append(field, aml_named_field("PRQ0", 8));
410     aml_append(field, aml_named_field("PRQ1", 8));
411     aml_append(field, aml_named_field("PRQ2", 8));
412     aml_append(field, aml_named_field("PRQ3", 8));
413     aml_append(sb_scope, field);
414     aml_append(scope, sb_scope);
415 
416     qbus_build_aml(bus, scope);
417 }
418 
419 static void pci_piix_init(Object *obj)
420 {
421     PIIXState *d = PIIX_PCI_DEVICE(obj);
422 
423     qdev_init_gpio_out_named(DEVICE(obj), d->isa_irqs_in, "isa-irqs",
424                              ISA_NUM_IRQS);
425 
426     object_initialize_child(obj, "rtc", &d->rtc, TYPE_MC146818_RTC);
427 }
428 
429 static Property pci_piix_props[] = {
430     DEFINE_PROP_UINT32("smb_io_base", PIIXState, smb_io_base, 0),
431     DEFINE_PROP_BOOL("has-acpi", PIIXState, has_acpi, true),
432     DEFINE_PROP_BOOL("has-pic", PIIXState, has_pic, true),
433     DEFINE_PROP_BOOL("has-pit", PIIXState, has_pit, true),
434     DEFINE_PROP_BOOL("has-usb", PIIXState, has_usb, true),
435     DEFINE_PROP_BOOL("smm-enabled", PIIXState, smm_enabled, false),
436     DEFINE_PROP_END_OF_LIST(),
437 };
438 
439 static void pci_piix_class_init(ObjectClass *klass, void *data)
440 {
441     DeviceClass *dc = DEVICE_CLASS(klass);
442     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
443     AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
444 
445     dc->reset       = piix_reset;
446     dc->desc        = "ISA bridge";
447     dc->hotpluggable   = false;
448     k->vendor_id    = PCI_VENDOR_ID_INTEL;
449     k->class_id     = PCI_CLASS_BRIDGE_ISA;
450     /*
451      * Reason: part of PIIX southbridge, needs to be wired up by e.g.
452      * pc_piix.c's pc_init1()
453      */
454     dc->user_creatable = false;
455     device_class_set_props(dc, pci_piix_props);
456     adevc->build_dev_aml = build_pci_isa_aml;
457 }
458 
459 static const TypeInfo piix_pci_type_info = {
460     .name = TYPE_PIIX_PCI_DEVICE,
461     .parent = TYPE_PCI_DEVICE,
462     .instance_size = sizeof(PIIXState),
463     .instance_init = pci_piix_init,
464     .abstract = true,
465     .class_init = pci_piix_class_init,
466     .interfaces = (InterfaceInfo[]) {
467         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
468         { TYPE_ACPI_DEV_AML_IF },
469         { },
470     },
471 };
472 
473 static void piix3_realize(PCIDevice *dev, Error **errp)
474 {
475     ERRP_GUARD();
476     PIIXState *piix3 = PIIX_PCI_DEVICE(dev);
477     PCIBus *pci_bus = pci_get_bus(dev);
478 
479     pci_piix_realize(dev, TYPE_PIIX3_USB_UHCI, errp);
480     if (*errp) {
481         return;
482     }
483 
484     pci_bus_irqs(pci_bus, piix3_set_irq, piix3, PIIX_NUM_PIRQS);
485     pci_bus_set_route_irq_fn(pci_bus, piix3_route_intx_pin_to_irq);
486 }
487 
488 static void piix3_init(Object *obj)
489 {
490     PIIXState *d = PIIX_PCI_DEVICE(obj);
491 
492     object_initialize_child(obj, "ide", &d->ide, TYPE_PIIX3_IDE);
493 }
494 
495 static void piix3_class_init(ObjectClass *klass, void *data)
496 {
497     DeviceClass *dc = DEVICE_CLASS(klass);
498     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
499 
500     k->config_write = piix3_write_config;
501     k->realize = piix3_realize;
502     /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
503     k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0;
504     dc->vmsd = &vmstate_piix3;
505 }
506 
507 static const TypeInfo piix3_info = {
508     .name          = TYPE_PIIX3_DEVICE,
509     .parent        = TYPE_PIIX_PCI_DEVICE,
510     .instance_init = piix3_init,
511     .class_init    = piix3_class_init,
512 };
513 
514 static void piix4_realize(PCIDevice *dev, Error **errp)
515 {
516     ERRP_GUARD();
517     PIIXState *s = PIIX_PCI_DEVICE(dev);
518     PCIBus *pci_bus = pci_get_bus(dev);
519 
520     pci_piix_realize(dev, TYPE_PIIX4_USB_UHCI, errp);
521     if (*errp) {
522         return;
523     }
524 
525     pci_bus_irqs(pci_bus, piix4_set_irq, s, PIIX_NUM_PIRQS);
526 }
527 
528 static void piix4_init(Object *obj)
529 {
530     PIIXState *s = PIIX_PCI_DEVICE(obj);
531 
532     object_initialize_child(obj, "ide", &s->ide, TYPE_PIIX4_IDE);
533 }
534 
535 static void piix4_class_init(ObjectClass *klass, void *data)
536 {
537     DeviceClass *dc = DEVICE_CLASS(klass);
538     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
539 
540     k->realize = piix4_realize;
541     k->device_id = PCI_DEVICE_ID_INTEL_82371AB_0;
542     dc->vmsd = &vmstate_piix4;
543 }
544 
545 static const TypeInfo piix4_info = {
546     .name          = TYPE_PIIX4_PCI_DEVICE,
547     .parent        = TYPE_PIIX_PCI_DEVICE,
548     .instance_init = piix4_init,
549     .class_init    = piix4_class_init,
550 };
551 
552 static void piix3_register_types(void)
553 {
554     type_register_static(&piix_pci_type_info);
555     type_register_static(&piix3_info);
556     type_register_static(&piix4_info);
557 }
558 
559 type_init(piix3_register_types)
560