i8254_common.c 7.8 KB

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