piix4.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * QEMU PIIX4 PCI 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. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "hw/irq.h"
  28. #include "hw/i386/pc.h"
  29. #include "hw/southbridge/piix.h"
  30. #include "hw/pci/pci.h"
  31. #include "hw/isa/isa.h"
  32. #include "hw/sysbus.h"
  33. #include "hw/dma/i8257.h"
  34. #include "hw/timer/i8254.h"
  35. #include "hw/rtc/mc146818rtc.h"
  36. #include "hw/ide.h"
  37. #include "migration/vmstate.h"
  38. #include "sysemu/reset.h"
  39. #include "sysemu/runstate.h"
  40. PCIDevice *piix4_dev;
  41. typedef struct PIIX4State {
  42. PCIDevice dev;
  43. qemu_irq cpu_intr;
  44. qemu_irq *isa;
  45. RTCState rtc;
  46. /* Reset Control Register */
  47. MemoryRegion rcr_mem;
  48. uint8_t rcr;
  49. } PIIX4State;
  50. #define PIIX4_PCI_DEVICE(obj) \
  51. OBJECT_CHECK(PIIX4State, (obj), TYPE_PIIX4_PCI_DEVICE)
  52. static void piix4_isa_reset(DeviceState *dev)
  53. {
  54. PIIX4State *d = PIIX4_PCI_DEVICE(dev);
  55. uint8_t *pci_conf = d->dev.config;
  56. pci_conf[0x04] = 0x07; // master, memory and I/O
  57. pci_conf[0x05] = 0x00;
  58. pci_conf[0x06] = 0x00;
  59. pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
  60. pci_conf[0x4c] = 0x4d;
  61. pci_conf[0x4e] = 0x03;
  62. pci_conf[0x4f] = 0x00;
  63. pci_conf[0x60] = 0x0a; // PCI A -> IRQ 10
  64. pci_conf[0x61] = 0x0a; // PCI B -> IRQ 10
  65. pci_conf[0x62] = 0x0b; // PCI C -> IRQ 11
  66. pci_conf[0x63] = 0x0b; // PCI D -> IRQ 11
  67. pci_conf[0x69] = 0x02;
  68. pci_conf[0x70] = 0x80;
  69. pci_conf[0x76] = 0x0c;
  70. pci_conf[0x77] = 0x0c;
  71. pci_conf[0x78] = 0x02;
  72. pci_conf[0x79] = 0x00;
  73. pci_conf[0x80] = 0x00;
  74. pci_conf[0x82] = 0x00;
  75. pci_conf[0xa0] = 0x08;
  76. pci_conf[0xa2] = 0x00;
  77. pci_conf[0xa3] = 0x00;
  78. pci_conf[0xa4] = 0x00;
  79. pci_conf[0xa5] = 0x00;
  80. pci_conf[0xa6] = 0x00;
  81. pci_conf[0xa7] = 0x00;
  82. pci_conf[0xa8] = 0x0f;
  83. pci_conf[0xaa] = 0x00;
  84. pci_conf[0xab] = 0x00;
  85. pci_conf[0xac] = 0x00;
  86. pci_conf[0xae] = 0x00;
  87. }
  88. static const VMStateDescription vmstate_piix4 = {
  89. .name = "PIIX4",
  90. .version_id = 2,
  91. .minimum_version_id = 2,
  92. .fields = (VMStateField[]) {
  93. VMSTATE_PCI_DEVICE(dev, PIIX4State),
  94. VMSTATE_END_OF_LIST()
  95. }
  96. };
  97. static void piix4_request_i8259_irq(void *opaque, int irq, int level)
  98. {
  99. PIIX4State *s = opaque;
  100. qemu_set_irq(s->cpu_intr, level);
  101. }
  102. static void piix4_set_i8259_irq(void *opaque, int irq, int level)
  103. {
  104. PIIX4State *s = opaque;
  105. qemu_set_irq(s->isa[irq], level);
  106. }
  107. static void piix4_rcr_write(void *opaque, hwaddr addr, uint64_t val,
  108. unsigned int len)
  109. {
  110. PIIX4State *s = opaque;
  111. if (val & 4) {
  112. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  113. return;
  114. }
  115. s->rcr = val & 2; /* keep System Reset type only */
  116. }
  117. static uint64_t piix4_rcr_read(void *opaque, hwaddr addr, unsigned int len)
  118. {
  119. PIIX4State *s = opaque;
  120. return s->rcr;
  121. }
  122. static const MemoryRegionOps piix4_rcr_ops = {
  123. .read = piix4_rcr_read,
  124. .write = piix4_rcr_write,
  125. .endianness = DEVICE_LITTLE_ENDIAN,
  126. .impl = {
  127. .min_access_size = 1,
  128. .max_access_size = 1,
  129. },
  130. };
  131. static void piix4_realize(PCIDevice *dev, Error **errp)
  132. {
  133. PIIX4State *s = PIIX4_PCI_DEVICE(dev);
  134. ISABus *isa_bus;
  135. qemu_irq *i8259_out_irq;
  136. Error *err = NULL;
  137. isa_bus = isa_bus_new(DEVICE(dev), pci_address_space(dev),
  138. pci_address_space_io(dev), errp);
  139. if (!isa_bus) {
  140. return;
  141. }
  142. qdev_init_gpio_in_named(DEVICE(dev), piix4_set_i8259_irq,
  143. "isa", ISA_NUM_IRQS);
  144. qdev_init_gpio_out_named(DEVICE(dev), &s->cpu_intr,
  145. "intr", 1);
  146. memory_region_init_io(&s->rcr_mem, OBJECT(dev), &piix4_rcr_ops, s,
  147. "reset-control", 1);
  148. memory_region_add_subregion_overlap(pci_address_space_io(dev),
  149. PIIX_RCR_IOPORT, &s->rcr_mem, 1);
  150. /* initialize i8259 pic */
  151. i8259_out_irq = qemu_allocate_irqs(piix4_request_i8259_irq, s, 1);
  152. s->isa = i8259_init(isa_bus, *i8259_out_irq);
  153. /* initialize ISA irqs */
  154. isa_bus_irqs(isa_bus, s->isa);
  155. /* initialize pit */
  156. i8254_pit_init(isa_bus, 0x40, 0, NULL);
  157. /* DMA */
  158. i8257_dma_init(isa_bus, 0);
  159. /* RTC */
  160. qdev_set_parent_bus(DEVICE(&s->rtc), BUS(isa_bus));
  161. qdev_prop_set_int32(DEVICE(&s->rtc), "base_year", 2000);
  162. object_property_set_bool(OBJECT(&s->rtc), true, "realized", &err);
  163. if (err) {
  164. error_propagate(errp, err);
  165. return;
  166. }
  167. isa_init_irq(ISA_DEVICE(&s->rtc), &s->rtc.irq, RTC_ISA_IRQ);
  168. piix4_dev = dev;
  169. }
  170. static void piix4_init(Object *obj)
  171. {
  172. PIIX4State *s = PIIX4_PCI_DEVICE(obj);
  173. object_initialize(&s->rtc, sizeof(s->rtc), TYPE_MC146818_RTC);
  174. }
  175. static void piix4_class_init(ObjectClass *klass, void *data)
  176. {
  177. DeviceClass *dc = DEVICE_CLASS(klass);
  178. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  179. k->realize = piix4_realize;
  180. k->vendor_id = PCI_VENDOR_ID_INTEL;
  181. k->device_id = PCI_DEVICE_ID_INTEL_82371AB_0;
  182. k->class_id = PCI_CLASS_BRIDGE_ISA;
  183. dc->reset = piix4_isa_reset;
  184. dc->desc = "ISA bridge";
  185. dc->vmsd = &vmstate_piix4;
  186. /*
  187. * Reason: part of PIIX4 southbridge, needs to be wired up,
  188. * e.g. by mips_malta_init()
  189. */
  190. dc->user_creatable = false;
  191. dc->hotpluggable = false;
  192. }
  193. static const TypeInfo piix4_info = {
  194. .name = TYPE_PIIX4_PCI_DEVICE,
  195. .parent = TYPE_PCI_DEVICE,
  196. .instance_size = sizeof(PIIX4State),
  197. .instance_init = piix4_init,
  198. .class_init = piix4_class_init,
  199. .interfaces = (InterfaceInfo[]) {
  200. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  201. { },
  202. },
  203. };
  204. static void piix4_register_types(void)
  205. {
  206. type_register_static(&piix4_info);
  207. }
  208. type_init(piix4_register_types)
  209. DeviceState *piix4_create(PCIBus *pci_bus, ISABus **isa_bus,
  210. I2CBus **smbus, size_t ide_buses)
  211. {
  212. size_t ide_drives = ide_buses * MAX_IDE_DEVS;
  213. DriveInfo **hd;
  214. PCIDevice *pci;
  215. DeviceState *dev;
  216. pci = pci_create_simple_multifunction(pci_bus, PCI_DEVFN(10, 0),
  217. true, TYPE_PIIX4_PCI_DEVICE);
  218. dev = DEVICE(pci);
  219. if (isa_bus) {
  220. *isa_bus = ISA_BUS(qdev_get_child_bus(dev, "isa.0"));
  221. }
  222. hd = g_new(DriveInfo *, ide_drives);
  223. ide_drive_get(hd, ide_drives);
  224. pci_piix4_ide_init(pci_bus, hd, pci->devfn + 1);
  225. g_free(hd);
  226. pci_create_simple(pci_bus, pci->devfn + 2, "piix4-usb-uhci");
  227. if (smbus) {
  228. *smbus = piix4_pm_init(pci_bus, pci->devfn + 3, 0x1100,
  229. isa_get_irq(NULL, 9), NULL, 0, NULL);
  230. }
  231. return dev;
  232. }