2
0

piix.c 15 KB

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