piix3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * QEMU PIIX PCI ISA Bridge Emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/range.h"
  26. #include "hw/southbridge/piix.h"
  27. #include "hw/irq.h"
  28. #include "hw/isa/isa.h"
  29. #include "hw/xen/xen.h"
  30. #include "sysemu/sysemu.h"
  31. #include "sysemu/reset.h"
  32. #include "sysemu/runstate.h"
  33. #include "migration/vmstate.h"
  34. #define XEN_PIIX_NUM_PIRQS 128ULL
  35. #define TYPE_PIIX3_PCI_DEVICE "pci-piix3"
  36. #define PIIX3_PCI_DEVICE(obj) \
  37. OBJECT_CHECK(PIIX3State, (obj), TYPE_PIIX3_PCI_DEVICE)
  38. #define TYPE_PIIX3_DEVICE "PIIX3"
  39. #define TYPE_PIIX3_XEN_DEVICE "PIIX3-xen"
  40. static void piix3_set_irq_pic(PIIX3State *piix3, int pic_irq)
  41. {
  42. qemu_set_irq(piix3->pic[pic_irq],
  43. !!(piix3->pic_levels &
  44. (((1ULL << PIIX_NUM_PIRQS) - 1) <<
  45. (pic_irq * PIIX_NUM_PIRQS))));
  46. }
  47. static void piix3_set_irq_level_internal(PIIX3State *piix3, int pirq, int level)
  48. {
  49. int pic_irq;
  50. uint64_t mask;
  51. pic_irq = piix3->dev.config[PIIX_PIRQCA + pirq];
  52. if (pic_irq >= PIIX_NUM_PIC_IRQS) {
  53. return;
  54. }
  55. mask = 1ULL << ((pic_irq * PIIX_NUM_PIRQS) + pirq);
  56. piix3->pic_levels &= ~mask;
  57. piix3->pic_levels |= mask * !!level;
  58. }
  59. static void piix3_set_irq_level(PIIX3State *piix3, int pirq, int level)
  60. {
  61. int pic_irq;
  62. pic_irq = piix3->dev.config[PIIX_PIRQCA + pirq];
  63. if (pic_irq >= PIIX_NUM_PIC_IRQS) {
  64. return;
  65. }
  66. piix3_set_irq_level_internal(piix3, pirq, level);
  67. piix3_set_irq_pic(piix3, pic_irq);
  68. }
  69. static void piix3_set_irq(void *opaque, int pirq, int level)
  70. {
  71. PIIX3State *piix3 = opaque;
  72. piix3_set_irq_level(piix3, pirq, level);
  73. }
  74. static PCIINTxRoute piix3_route_intx_pin_to_irq(void *opaque, int pin)
  75. {
  76. PIIX3State *piix3 = opaque;
  77. int irq = piix3->dev.config[PIIX_PIRQCA + pin];
  78. PCIINTxRoute route;
  79. if (irq < PIIX_NUM_PIC_IRQS) {
  80. route.mode = PCI_INTX_ENABLED;
  81. route.irq = irq;
  82. } else {
  83. route.mode = PCI_INTX_DISABLED;
  84. route.irq = -1;
  85. }
  86. return route;
  87. }
  88. /* irq routing is changed. so rebuild bitmap */
  89. static void piix3_update_irq_levels(PIIX3State *piix3)
  90. {
  91. PCIBus *bus = pci_get_bus(&piix3->dev);
  92. int pirq;
  93. piix3->pic_levels = 0;
  94. for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
  95. piix3_set_irq_level(piix3, pirq, pci_bus_get_irq_level(bus, pirq));
  96. }
  97. }
  98. static void piix3_write_config(PCIDevice *dev,
  99. uint32_t address, uint32_t val, int len)
  100. {
  101. pci_default_write_config(dev, address, val, len);
  102. if (ranges_overlap(address, len, PIIX_PIRQCA, 4)) {
  103. PIIX3State *piix3 = PIIX3_PCI_DEVICE(dev);
  104. int pic_irq;
  105. pci_bus_fire_intx_routing_notifier(pci_get_bus(&piix3->dev));
  106. piix3_update_irq_levels(piix3);
  107. for (pic_irq = 0; pic_irq < PIIX_NUM_PIC_IRQS; pic_irq++) {
  108. piix3_set_irq_pic(piix3, pic_irq);
  109. }
  110. }
  111. }
  112. static void piix3_write_config_xen(PCIDevice *dev,
  113. uint32_t address, uint32_t val, int len)
  114. {
  115. xen_piix_pci_write_config_client(address, val, len);
  116. piix3_write_config(dev, address, val, len);
  117. }
  118. static void piix3_reset(void *opaque)
  119. {
  120. PIIX3State *d = opaque;
  121. uint8_t *pci_conf = d->dev.config;
  122. pci_conf[0x04] = 0x07; /* master, memory and I/O */
  123. pci_conf[0x05] = 0x00;
  124. pci_conf[0x06] = 0x00;
  125. pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
  126. pci_conf[0x4c] = 0x4d;
  127. pci_conf[0x4e] = 0x03;
  128. pci_conf[0x4f] = 0x00;
  129. pci_conf[0x60] = 0x80;
  130. pci_conf[0x61] = 0x80;
  131. pci_conf[0x62] = 0x80;
  132. pci_conf[0x63] = 0x80;
  133. pci_conf[0x69] = 0x02;
  134. pci_conf[0x70] = 0x80;
  135. pci_conf[0x76] = 0x0c;
  136. pci_conf[0x77] = 0x0c;
  137. pci_conf[0x78] = 0x02;
  138. pci_conf[0x79] = 0x00;
  139. pci_conf[0x80] = 0x00;
  140. pci_conf[0x82] = 0x00;
  141. pci_conf[0xa0] = 0x08;
  142. pci_conf[0xa2] = 0x00;
  143. pci_conf[0xa3] = 0x00;
  144. pci_conf[0xa4] = 0x00;
  145. pci_conf[0xa5] = 0x00;
  146. pci_conf[0xa6] = 0x00;
  147. pci_conf[0xa7] = 0x00;
  148. pci_conf[0xa8] = 0x0f;
  149. pci_conf[0xaa] = 0x00;
  150. pci_conf[0xab] = 0x00;
  151. pci_conf[0xac] = 0x00;
  152. pci_conf[0xae] = 0x00;
  153. d->pic_levels = 0;
  154. d->rcr = 0;
  155. }
  156. static int piix3_post_load(void *opaque, int version_id)
  157. {
  158. PIIX3State *piix3 = opaque;
  159. int pirq;
  160. /*
  161. * Because the i8259 has not been deserialized yet, qemu_irq_raise
  162. * might bring the system to a different state than the saved one;
  163. * for example, the interrupt could be masked but the i8259 would
  164. * not know that yet and would trigger an interrupt in the CPU.
  165. *
  166. * Here, we update irq levels without raising the interrupt.
  167. * Interrupt state will be deserialized separately through the i8259.
  168. */
  169. piix3->pic_levels = 0;
  170. for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
  171. piix3_set_irq_level_internal(piix3, pirq,
  172. pci_bus_get_irq_level(pci_get_bus(&piix3->dev), pirq));
  173. }
  174. return 0;
  175. }
  176. static int piix3_pre_save(void *opaque)
  177. {
  178. int i;
  179. PIIX3State *piix3 = opaque;
  180. for (i = 0; i < ARRAY_SIZE(piix3->pci_irq_levels_vmstate); i++) {
  181. piix3->pci_irq_levels_vmstate[i] =
  182. pci_bus_get_irq_level(pci_get_bus(&piix3->dev), i);
  183. }
  184. return 0;
  185. }
  186. static bool piix3_rcr_needed(void *opaque)
  187. {
  188. PIIX3State *piix3 = opaque;
  189. return (piix3->rcr != 0);
  190. }
  191. static const VMStateDescription vmstate_piix3_rcr = {
  192. .name = "PIIX3/rcr",
  193. .version_id = 1,
  194. .minimum_version_id = 1,
  195. .needed = piix3_rcr_needed,
  196. .fields = (VMStateField[]) {
  197. VMSTATE_UINT8(rcr, PIIX3State),
  198. VMSTATE_END_OF_LIST()
  199. }
  200. };
  201. static const VMStateDescription vmstate_piix3 = {
  202. .name = "PIIX3",
  203. .version_id = 3,
  204. .minimum_version_id = 2,
  205. .post_load = piix3_post_load,
  206. .pre_save = piix3_pre_save,
  207. .fields = (VMStateField[]) {
  208. VMSTATE_PCI_DEVICE(dev, PIIX3State),
  209. VMSTATE_INT32_ARRAY_V(pci_irq_levels_vmstate, PIIX3State,
  210. PIIX_NUM_PIRQS, 3),
  211. VMSTATE_END_OF_LIST()
  212. },
  213. .subsections = (const VMStateDescription*[]) {
  214. &vmstate_piix3_rcr,
  215. NULL
  216. }
  217. };
  218. static void rcr_write(void *opaque, hwaddr addr, uint64_t val, unsigned len)
  219. {
  220. PIIX3State *d = opaque;
  221. if (val & 4) {
  222. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  223. return;
  224. }
  225. d->rcr = val & 2; /* keep System Reset type only */
  226. }
  227. static uint64_t rcr_read(void *opaque, hwaddr addr, unsigned len)
  228. {
  229. PIIX3State *d = opaque;
  230. return d->rcr;
  231. }
  232. static const MemoryRegionOps rcr_ops = {
  233. .read = rcr_read,
  234. .write = rcr_write,
  235. .endianness = DEVICE_LITTLE_ENDIAN
  236. };
  237. static void piix3_realize(PCIDevice *dev, Error **errp)
  238. {
  239. PIIX3State *d = PIIX3_PCI_DEVICE(dev);
  240. if (!isa_bus_new(DEVICE(d), get_system_memory(),
  241. pci_address_space_io(dev), errp)) {
  242. return;
  243. }
  244. memory_region_init_io(&d->rcr_mem, OBJECT(dev), &rcr_ops, d,
  245. "piix3-reset-control", 1);
  246. memory_region_add_subregion_overlap(pci_address_space_io(dev),
  247. PIIX_RCR_IOPORT, &d->rcr_mem, 1);
  248. qemu_register_reset(piix3_reset, d);
  249. }
  250. static void pci_piix3_class_init(ObjectClass *klass, void *data)
  251. {
  252. DeviceClass *dc = DEVICE_CLASS(klass);
  253. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  254. dc->desc = "ISA bridge";
  255. dc->vmsd = &vmstate_piix3;
  256. dc->hotpluggable = false;
  257. k->realize = piix3_realize;
  258. k->vendor_id = PCI_VENDOR_ID_INTEL;
  259. /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
  260. k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0;
  261. k->class_id = PCI_CLASS_BRIDGE_ISA;
  262. /*
  263. * Reason: part of PIIX3 southbridge, needs to be wired up by
  264. * pc_piix.c's pc_init1()
  265. */
  266. dc->user_creatable = false;
  267. }
  268. static const TypeInfo piix3_pci_type_info = {
  269. .name = TYPE_PIIX3_PCI_DEVICE,
  270. .parent = TYPE_PCI_DEVICE,
  271. .instance_size = sizeof(PIIX3State),
  272. .abstract = true,
  273. .class_init = pci_piix3_class_init,
  274. .interfaces = (InterfaceInfo[]) {
  275. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  276. { },
  277. },
  278. };
  279. static void piix3_class_init(ObjectClass *klass, void *data)
  280. {
  281. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  282. k->config_write = piix3_write_config;
  283. }
  284. static const TypeInfo piix3_info = {
  285. .name = TYPE_PIIX3_DEVICE,
  286. .parent = TYPE_PIIX3_PCI_DEVICE,
  287. .class_init = piix3_class_init,
  288. };
  289. static void piix3_xen_class_init(ObjectClass *klass, void *data)
  290. {
  291. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  292. k->config_write = piix3_write_config_xen;
  293. };
  294. static const TypeInfo piix3_xen_info = {
  295. .name = TYPE_PIIX3_XEN_DEVICE,
  296. .parent = TYPE_PIIX3_PCI_DEVICE,
  297. .class_init = piix3_xen_class_init,
  298. };
  299. static void piix3_register_types(void)
  300. {
  301. type_register_static(&piix3_pci_type_info);
  302. type_register_static(&piix3_info);
  303. type_register_static(&piix3_xen_info);
  304. }
  305. type_init(piix3_register_types)
  306. /*
  307. * Return the global irq number corresponding to a given device irq
  308. * pin. We could also use the bus number to have a more precise mapping.
  309. */
  310. static int pci_slot_get_pirq(PCIDevice *pci_dev, int pci_intx)
  311. {
  312. int slot_addend;
  313. slot_addend = (pci_dev->devfn >> 3) - 1;
  314. return (pci_intx + slot_addend) & 3;
  315. }
  316. PIIX3State *piix3_create(PCIBus *pci_bus, ISABus **isa_bus)
  317. {
  318. PIIX3State *piix3;
  319. PCIDevice *pci_dev;
  320. /*
  321. * Xen supports additional interrupt routes from the PCI devices to
  322. * the IOAPIC: the four pins of each PCI device on the bus are also
  323. * connected to the IOAPIC directly.
  324. * These additional routes can be discovered through ACPI.
  325. */
  326. if (xen_enabled()) {
  327. pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
  328. TYPE_PIIX3_XEN_DEVICE);
  329. piix3 = PIIX3_PCI_DEVICE(pci_dev);
  330. pci_bus_irqs(pci_bus, xen_piix3_set_irq, xen_pci_slot_get_pirq,
  331. piix3, XEN_PIIX_NUM_PIRQS);
  332. } else {
  333. pci_dev = pci_create_simple_multifunction(pci_bus, -1, true,
  334. TYPE_PIIX3_DEVICE);
  335. piix3 = PIIX3_PCI_DEVICE(pci_dev);
  336. pci_bus_irqs(pci_bus, piix3_set_irq, pci_slot_get_pirq,
  337. piix3, PIIX_NUM_PIRQS);
  338. pci_bus_set_route_irq_fn(pci_bus, piix3_route_intx_pin_to_irq);
  339. }
  340. *isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(piix3), "isa.0"));
  341. return piix3;
  342. }