piix3.c 11 KB

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