i8259_common.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * QEMU 8259 - common bits of emulated and KVM kernel model
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2011 Jan Kiszka, Siemens AG
  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 "hw/intc/i8259.h"
  27. #include "hw/isa/i8259_internal.h"
  28. #include "hw/qdev-properties.h"
  29. #include "migration/vmstate.h"
  30. #include "qapi/error.h"
  31. static int irq_level[16];
  32. static uint64_t irq_count[16];
  33. void pic_reset_common(PICCommonState *s)
  34. {
  35. s->last_irr = 0;
  36. s->irr &= s->elcr;
  37. s->imr = 0;
  38. s->isr = 0;
  39. s->priority_add = 0;
  40. s->irq_base = 0;
  41. s->read_reg_select = 0;
  42. s->poll = 0;
  43. s->special_mask = 0;
  44. s->init_state = 0;
  45. s->auto_eoi = 0;
  46. s->rotate_on_auto_eoi = 0;
  47. s->special_fully_nested_mode = 0;
  48. s->init4 = 0;
  49. s->single_mode = 0;
  50. /* Note: ELCR and LTIM are not reset */
  51. }
  52. static int pic_dispatch_pre_save(void *opaque)
  53. {
  54. PICCommonState *s = opaque;
  55. PICCommonClass *info = PIC_COMMON_GET_CLASS(s);
  56. if (info->pre_save) {
  57. info->pre_save(s);
  58. }
  59. return 0;
  60. }
  61. static int pic_dispatch_post_load(void *opaque, int version_id)
  62. {
  63. PICCommonState *s = opaque;
  64. PICCommonClass *info = PIC_COMMON_GET_CLASS(s);
  65. if (info->post_load) {
  66. info->post_load(s);
  67. }
  68. return 0;
  69. }
  70. static void pic_common_realize(DeviceState *dev, Error **errp)
  71. {
  72. PICCommonState *s = PIC_COMMON(dev);
  73. ISADevice *isa = ISA_DEVICE(dev);
  74. isa_register_ioport(isa, &s->base_io, s->iobase);
  75. if (s->elcr_addr != -1) {
  76. isa_register_ioport(isa, &s->elcr_io, s->elcr_addr);
  77. }
  78. qdev_set_legacy_instance_id(dev, s->iobase, 1);
  79. }
  80. ISADevice *i8259_init_chip(const char *name, ISABus *bus, bool master)
  81. {
  82. DeviceState *dev;
  83. ISADevice *isadev;
  84. isadev = isa_new(name);
  85. dev = DEVICE(isadev);
  86. qdev_prop_set_uint32(dev, "iobase", master ? 0x20 : 0xa0);
  87. qdev_prop_set_uint32(dev, "elcr_addr", master ? 0x4d0 : 0x4d1);
  88. qdev_prop_set_uint8(dev, "elcr_mask", master ? 0xf8 : 0xde);
  89. qdev_prop_set_bit(dev, "master", master);
  90. isa_realize_and_unref(isadev, bus, &error_fatal);
  91. return isadev;
  92. }
  93. void pic_stat_update_irq(int irq, int level)
  94. {
  95. if (level != irq_level[irq]) {
  96. irq_level[irq] = level;
  97. if (level == 1) {
  98. irq_count[irq]++;
  99. }
  100. }
  101. }
  102. static bool pic_get_statistics(InterruptStatsProvider *obj,
  103. uint64_t **irq_counts, unsigned int *nb_irqs)
  104. {
  105. PICCommonState *s = PIC_COMMON(obj);
  106. if (s->master) {
  107. *irq_counts = irq_count;
  108. *nb_irqs = ARRAY_SIZE(irq_count);
  109. } else {
  110. *irq_counts = NULL;
  111. *nb_irqs = 0;
  112. }
  113. return true;
  114. }
  115. static void pic_print_info(InterruptStatsProvider *obj, GString *buf)
  116. {
  117. PICCommonState *s = PIC_COMMON(obj);
  118. pic_dispatch_pre_save(s);
  119. g_string_append_printf(buf, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
  120. "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
  121. s->master ? 0 : 1, s->irr, s->imr, s->isr,
  122. s->priority_add,
  123. s->irq_base, s->read_reg_select, s->elcr,
  124. s->special_fully_nested_mode);
  125. }
  126. static bool ltim_state_needed(void *opaque)
  127. {
  128. PICCommonState *s = PIC_COMMON(opaque);
  129. return !!s->ltim;
  130. }
  131. static const VMStateDescription vmstate_pic_ltim = {
  132. .name = "i8259/ltim",
  133. .version_id = 1,
  134. .minimum_version_id = 1,
  135. .needed = ltim_state_needed,
  136. .fields = (const VMStateField[]) {
  137. VMSTATE_UINT8(ltim, PICCommonState),
  138. VMSTATE_END_OF_LIST()
  139. }
  140. };
  141. static const VMStateDescription vmstate_pic_common = {
  142. .name = "i8259",
  143. .version_id = 1,
  144. .minimum_version_id = 1,
  145. .pre_save = pic_dispatch_pre_save,
  146. .post_load = pic_dispatch_post_load,
  147. .fields = (const VMStateField[]) {
  148. VMSTATE_UINT8(last_irr, PICCommonState),
  149. VMSTATE_UINT8(irr, PICCommonState),
  150. VMSTATE_UINT8(imr, PICCommonState),
  151. VMSTATE_UINT8(isr, PICCommonState),
  152. VMSTATE_UINT8(priority_add, PICCommonState),
  153. VMSTATE_UINT8(irq_base, PICCommonState),
  154. VMSTATE_UINT8(read_reg_select, PICCommonState),
  155. VMSTATE_UINT8(poll, PICCommonState),
  156. VMSTATE_UINT8(special_mask, PICCommonState),
  157. VMSTATE_UINT8(init_state, PICCommonState),
  158. VMSTATE_UINT8(auto_eoi, PICCommonState),
  159. VMSTATE_UINT8(rotate_on_auto_eoi, PICCommonState),
  160. VMSTATE_UINT8(special_fully_nested_mode, PICCommonState),
  161. VMSTATE_UINT8(init4, PICCommonState),
  162. VMSTATE_UINT8(single_mode, PICCommonState),
  163. VMSTATE_UINT8(elcr, PICCommonState),
  164. VMSTATE_END_OF_LIST()
  165. },
  166. .subsections = (const VMStateDescription * const []) {
  167. &vmstate_pic_ltim,
  168. NULL
  169. }
  170. };
  171. static const Property pic_properties_common[] = {
  172. DEFINE_PROP_UINT32("iobase", PICCommonState, iobase, -1),
  173. DEFINE_PROP_UINT32("elcr_addr", PICCommonState, elcr_addr, -1),
  174. DEFINE_PROP_UINT8("elcr_mask", PICCommonState, elcr_mask, -1),
  175. DEFINE_PROP_BIT("master", PICCommonState, master, 0, false),
  176. };
  177. static void pic_common_class_init(ObjectClass *klass, void *data)
  178. {
  179. DeviceClass *dc = DEVICE_CLASS(klass);
  180. InterruptStatsProviderClass *ic = INTERRUPT_STATS_PROVIDER_CLASS(klass);
  181. dc->vmsd = &vmstate_pic_common;
  182. device_class_set_props(dc, pic_properties_common);
  183. dc->realize = pic_common_realize;
  184. /*
  185. * Reason: unlike ordinary ISA devices, the PICs need additional
  186. * wiring: its IRQ input lines are set up by board code, and the
  187. * wiring of the slave to the master is hard-coded in device model
  188. * code.
  189. */
  190. dc->user_creatable = false;
  191. ic->get_statistics = pic_get_statistics;
  192. ic->print_info = pic_print_info;
  193. }
  194. static const TypeInfo pic_common_type = {
  195. .name = TYPE_PIC_COMMON,
  196. .parent = TYPE_ISA_DEVICE,
  197. .instance_size = sizeof(PICCommonState),
  198. .class_size = sizeof(PICCommonClass),
  199. .class_init = pic_common_class_init,
  200. .abstract = true,
  201. .interfaces = (InterfaceInfo[]) {
  202. { TYPE_INTERRUPT_STATS_PROVIDER },
  203. { }
  204. },
  205. };
  206. static void pic_common_register_types(void)
  207. {
  208. type_register_static(&pic_common_type);
  209. }
  210. type_init(pic_common_register_types)