i8254_common.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * QEMU 8253/8254 - common bits of emulated and KVM kernel model
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2012 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/isa/isa.h"
  27. #include "qemu/module.h"
  28. #include "qemu/timer.h"
  29. #include "hw/timer/i8254.h"
  30. #include "hw/timer/i8254_internal.h"
  31. #include "migration/vmstate.h"
  32. /* val must be 0 or 1 */
  33. void pit_set_gate(ISADevice *dev, int channel, int val)
  34. {
  35. PITCommonState *pit = PIT_COMMON(dev);
  36. PITChannelState *s = &pit->channels[channel];
  37. PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
  38. c->set_channel_gate(pit, s, val);
  39. }
  40. /* get pit output bit */
  41. int pit_get_out(PITChannelState *s, int64_t current_time)
  42. {
  43. uint64_t d;
  44. int out;
  45. d = muldiv64(current_time - s->count_load_time, PIT_FREQ,
  46. NANOSECONDS_PER_SECOND);
  47. switch (s->mode) {
  48. default:
  49. case 0:
  50. case 1:
  51. out = (d >= s->count);
  52. break;
  53. case 2:
  54. if ((d % s->count) == 0 && d != 0) {
  55. out = 1;
  56. } else {
  57. out = 0;
  58. }
  59. break;
  60. case 3:
  61. out = (d % s->count) < ((s->count + 1) >> 1);
  62. break;
  63. case 4:
  64. case 5:
  65. out = (d == s->count);
  66. break;
  67. }
  68. return out;
  69. }
  70. /* return -1 if no transition will occur. */
  71. int64_t pit_get_next_transition_time(PITChannelState *s, int64_t current_time)
  72. {
  73. uint64_t d, next_time, base;
  74. int period2;
  75. d = muldiv64(current_time - s->count_load_time, PIT_FREQ,
  76. NANOSECONDS_PER_SECOND);
  77. switch (s->mode) {
  78. default:
  79. case 0:
  80. case 1:
  81. if (d < s->count) {
  82. next_time = s->count;
  83. } else {
  84. return -1;
  85. }
  86. break;
  87. case 2:
  88. base = QEMU_ALIGN_DOWN(d, s->count);
  89. if ((d - base) == 0 && d != 0) {
  90. next_time = base + s->count;
  91. } else {
  92. next_time = base + s->count + 1;
  93. }
  94. break;
  95. case 3:
  96. base = QEMU_ALIGN_DOWN(d, s->count);
  97. period2 = ((s->count + 1) >> 1);
  98. if ((d - base) < period2) {
  99. next_time = base + period2;
  100. } else {
  101. next_time = base + s->count;
  102. }
  103. break;
  104. case 4:
  105. case 5:
  106. if (d < s->count) {
  107. next_time = s->count;
  108. } else if (d == s->count) {
  109. next_time = s->count + 1;
  110. } else {
  111. return -1;
  112. }
  113. break;
  114. }
  115. /* convert to timer units */
  116. next_time = s->count_load_time + muldiv64(next_time, NANOSECONDS_PER_SECOND,
  117. PIT_FREQ);
  118. /* fix potential rounding problems */
  119. /* XXX: better solution: use a clock at PIT_FREQ Hz */
  120. if (next_time <= current_time) {
  121. next_time = current_time + 1;
  122. }
  123. return next_time;
  124. }
  125. void pit_get_channel_info_common(PITCommonState *s, PITChannelState *sc,
  126. PITChannelInfo *info)
  127. {
  128. info->gate = sc->gate;
  129. info->mode = sc->mode;
  130. info->initial_count = sc->count;
  131. info->out = pit_get_out(sc, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  132. }
  133. void pit_get_channel_info(ISADevice *dev, int channel, PITChannelInfo *info)
  134. {
  135. PITCommonState *pit = PIT_COMMON(dev);
  136. PITChannelState *s = &pit->channels[channel];
  137. PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
  138. c->get_channel_info(pit, s, info);
  139. }
  140. void pit_reset_common(PITCommonState *pit)
  141. {
  142. PITChannelState *s;
  143. int i;
  144. for (i = 0; i < 3; i++) {
  145. s = &pit->channels[i];
  146. s->mode = 3;
  147. s->gate = (i != 2);
  148. s->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  149. s->count = 0x10000;
  150. if (i == 0 && !s->irq_disabled) {
  151. s->next_transition_time =
  152. pit_get_next_transition_time(s, s->count_load_time);
  153. }
  154. }
  155. }
  156. static void pit_common_realize(DeviceState *dev, Error **errp)
  157. {
  158. ISADevice *isadev = ISA_DEVICE(dev);
  159. PITCommonState *pit = PIT_COMMON(dev);
  160. isa_register_ioport(isadev, &pit->ioports, pit->iobase);
  161. qdev_set_legacy_instance_id(dev, pit->iobase, 2);
  162. }
  163. static const VMStateDescription vmstate_pit_channel = {
  164. .name = "pit channel",
  165. .version_id = 2,
  166. .minimum_version_id = 2,
  167. .fields = (const VMStateField[]) {
  168. VMSTATE_INT32(count, PITChannelState),
  169. VMSTATE_UINT16(latched_count, PITChannelState),
  170. VMSTATE_UINT8(count_latched, PITChannelState),
  171. VMSTATE_UINT8(status_latched, PITChannelState),
  172. VMSTATE_UINT8(status, PITChannelState),
  173. VMSTATE_UINT8(read_state, PITChannelState),
  174. VMSTATE_UINT8(write_state, PITChannelState),
  175. VMSTATE_UINT8(write_latch, PITChannelState),
  176. VMSTATE_UINT8(rw_mode, PITChannelState),
  177. VMSTATE_UINT8(mode, PITChannelState),
  178. VMSTATE_UINT8(bcd, PITChannelState),
  179. VMSTATE_UINT8(gate, PITChannelState),
  180. VMSTATE_INT64(count_load_time, PITChannelState),
  181. VMSTATE_INT64(next_transition_time, PITChannelState),
  182. VMSTATE_END_OF_LIST()
  183. }
  184. };
  185. static int pit_dispatch_pre_save(void *opaque)
  186. {
  187. PITCommonState *s = opaque;
  188. PITCommonClass *c = PIT_COMMON_GET_CLASS(s);
  189. if (c->pre_save) {
  190. c->pre_save(s);
  191. }
  192. return 0;
  193. }
  194. static int pit_dispatch_post_load(void *opaque, int version_id)
  195. {
  196. PITCommonState *s = opaque;
  197. PITCommonClass *c = PIT_COMMON_GET_CLASS(s);
  198. if (c->post_load) {
  199. c->post_load(s);
  200. }
  201. return 0;
  202. }
  203. static const VMStateDescription vmstate_pit_common = {
  204. .name = "i8254",
  205. .version_id = 3,
  206. .minimum_version_id = 2,
  207. .pre_save = pit_dispatch_pre_save,
  208. .post_load = pit_dispatch_post_load,
  209. .fields = (const VMStateField[]) {
  210. VMSTATE_UINT32_V(channels[0].irq_disabled, PITCommonState, 3),
  211. VMSTATE_STRUCT_ARRAY(channels, PITCommonState, 3, 2,
  212. vmstate_pit_channel, PITChannelState),
  213. VMSTATE_INT64(channels[0].next_transition_time,
  214. PITCommonState), /* formerly irq_timer */
  215. VMSTATE_END_OF_LIST()
  216. }
  217. };
  218. static const Property pit_common_properties[] = {
  219. DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1),
  220. };
  221. static void pit_common_class_init(ObjectClass *klass, void *data)
  222. {
  223. DeviceClass *dc = DEVICE_CLASS(klass);
  224. dc->realize = pit_common_realize;
  225. dc->vmsd = &vmstate_pit_common;
  226. /*
  227. * Reason: unlike ordinary ISA devices, the PIT may need to be
  228. * wired to the HPET, and because of that, some wiring is always
  229. * done by board code.
  230. */
  231. dc->user_creatable = false;
  232. device_class_set_props(dc, pit_common_properties);
  233. }
  234. static const TypeInfo pit_common_type = {
  235. .name = TYPE_PIT_COMMON,
  236. .parent = TYPE_ISA_DEVICE,
  237. .instance_size = sizeof(PITCommonState),
  238. .class_size = sizeof(PITCommonClass),
  239. .class_init = pit_common_class_init,
  240. .abstract = true,
  241. };
  242. static void register_devices(void)
  243. {
  244. type_register_static(&pit_common_type);
  245. }
  246. type_init(register_devices);