i8254_common.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "hw.h"
  26. #include "pc.h"
  27. #include "isa.h"
  28. #include "qemu/timer.h"
  29. #include "i8254.h"
  30. #include "i8254_internal.h"
  31. /* val must be 0 or 1 */
  32. void pit_set_gate(ISADevice *dev, int channel, int val)
  33. {
  34. PITCommonState *pit = PIT_COMMON(dev);
  35. PITChannelState *s = &pit->channels[channel];
  36. PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
  37. c->set_channel_gate(pit, s, val);
  38. }
  39. /* get pit output bit */
  40. int pit_get_out(PITChannelState *s, int64_t current_time)
  41. {
  42. uint64_t d;
  43. int out;
  44. d = muldiv64(current_time - s->count_load_time, PIT_FREQ,
  45. get_ticks_per_sec());
  46. switch (s->mode) {
  47. default:
  48. case 0:
  49. out = (d >= s->count);
  50. break;
  51. case 1:
  52. out = (d < s->count);
  53. break;
  54. case 2:
  55. if ((d % s->count) == 0 && d != 0) {
  56. out = 1;
  57. } else {
  58. out = 0;
  59. }
  60. break;
  61. case 3:
  62. out = (d % s->count) < ((s->count + 1) >> 1);
  63. break;
  64. case 4:
  65. case 5:
  66. out = (d == s->count);
  67. break;
  68. }
  69. return out;
  70. }
  71. /* return -1 if no transition will occur. */
  72. int64_t pit_get_next_transition_time(PITChannelState *s, int64_t current_time)
  73. {
  74. uint64_t d, next_time, base;
  75. int period2;
  76. d = muldiv64(current_time - s->count_load_time, PIT_FREQ,
  77. get_ticks_per_sec());
  78. switch (s->mode) {
  79. default:
  80. case 0:
  81. case 1:
  82. if (d < s->count) {
  83. next_time = s->count;
  84. } else {
  85. return -1;
  86. }
  87. break;
  88. case 2:
  89. base = (d / s->count) * s->count;
  90. if ((d - base) == 0 && d != 0) {
  91. next_time = base + s->count;
  92. } else {
  93. next_time = base + s->count + 1;
  94. }
  95. break;
  96. case 3:
  97. base = (d / s->count) * s->count;
  98. period2 = ((s->count + 1) >> 1);
  99. if ((d - base) < period2) {
  100. next_time = base + period2;
  101. } else {
  102. next_time = base + s->count;
  103. }
  104. break;
  105. case 4:
  106. case 5:
  107. if (d < s->count) {
  108. next_time = s->count;
  109. } else if (d == s->count) {
  110. next_time = s->count + 1;
  111. } else {
  112. return -1;
  113. }
  114. break;
  115. }
  116. /* convert to timer units */
  117. next_time = s->count_load_time + muldiv64(next_time, get_ticks_per_sec(),
  118. PIT_FREQ);
  119. /* fix potential rounding problems */
  120. /* XXX: better solution: use a clock at PIT_FREQ Hz */
  121. if (next_time <= current_time) {
  122. next_time = current_time + 1;
  123. }
  124. return next_time;
  125. }
  126. void pit_get_channel_info_common(PITCommonState *s, PITChannelState *sc,
  127. PITChannelInfo *info)
  128. {
  129. info->gate = sc->gate;
  130. info->mode = sc->mode;
  131. info->initial_count = sc->count;
  132. info->out = pit_get_out(sc, qemu_get_clock_ns(vm_clock));
  133. }
  134. void pit_get_channel_info(ISADevice *dev, int channel, PITChannelInfo *info)
  135. {
  136. PITCommonState *pit = PIT_COMMON(dev);
  137. PITChannelState *s = &pit->channels[channel];
  138. PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
  139. c->get_channel_info(pit, s, info);
  140. }
  141. void pit_reset_common(PITCommonState *pit)
  142. {
  143. PITChannelState *s;
  144. int i;
  145. for (i = 0; i < 3; i++) {
  146. s = &pit->channels[i];
  147. s->mode = 3;
  148. s->gate = (i != 2);
  149. s->count_load_time = qemu_get_clock_ns(vm_clock);
  150. s->count = 0x10000;
  151. if (i == 0 && !s->irq_disabled) {
  152. s->next_transition_time =
  153. pit_get_next_transition_time(s, s->count_load_time);
  154. }
  155. }
  156. }
  157. static int pit_init_common(ISADevice *dev)
  158. {
  159. PITCommonState *pit = PIT_COMMON(dev);
  160. PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
  161. int ret;
  162. ret = c->init(pit);
  163. if (ret < 0) {
  164. return ret;
  165. }
  166. isa_register_ioport(dev, &pit->ioports, pit->iobase);
  167. qdev_set_legacy_instance_id(&dev->qdev, pit->iobase, 2);
  168. return 0;
  169. }
  170. static const VMStateDescription vmstate_pit_channel = {
  171. .name = "pit channel",
  172. .version_id = 2,
  173. .minimum_version_id = 2,
  174. .minimum_version_id_old = 2,
  175. .fields = (VMStateField[]) {
  176. VMSTATE_INT32(count, PITChannelState),
  177. VMSTATE_UINT16(latched_count, PITChannelState),
  178. VMSTATE_UINT8(count_latched, PITChannelState),
  179. VMSTATE_UINT8(status_latched, PITChannelState),
  180. VMSTATE_UINT8(status, PITChannelState),
  181. VMSTATE_UINT8(read_state, PITChannelState),
  182. VMSTATE_UINT8(write_state, PITChannelState),
  183. VMSTATE_UINT8(write_latch, PITChannelState),
  184. VMSTATE_UINT8(rw_mode, PITChannelState),
  185. VMSTATE_UINT8(mode, PITChannelState),
  186. VMSTATE_UINT8(bcd, PITChannelState),
  187. VMSTATE_UINT8(gate, PITChannelState),
  188. VMSTATE_INT64(count_load_time, PITChannelState),
  189. VMSTATE_INT64(next_transition_time, PITChannelState),
  190. VMSTATE_END_OF_LIST()
  191. }
  192. };
  193. static int pit_load_old(QEMUFile *f, void *opaque, int version_id)
  194. {
  195. PITCommonState *pit = opaque;
  196. PITCommonClass *c = PIT_COMMON_GET_CLASS(pit);
  197. PITChannelState *s;
  198. int i;
  199. if (version_id != 1) {
  200. return -EINVAL;
  201. }
  202. for (i = 0; i < 3; i++) {
  203. s = &pit->channels[i];
  204. s->count = qemu_get_be32(f);
  205. qemu_get_be16s(f, &s->latched_count);
  206. qemu_get_8s(f, &s->count_latched);
  207. qemu_get_8s(f, &s->status_latched);
  208. qemu_get_8s(f, &s->status);
  209. qemu_get_8s(f, &s->read_state);
  210. qemu_get_8s(f, &s->write_state);
  211. qemu_get_8s(f, &s->write_latch);
  212. qemu_get_8s(f, &s->rw_mode);
  213. qemu_get_8s(f, &s->mode);
  214. qemu_get_8s(f, &s->bcd);
  215. qemu_get_8s(f, &s->gate);
  216. s->count_load_time = qemu_get_be64(f);
  217. s->irq_disabled = 0;
  218. if (i == 0) {
  219. s->next_transition_time = qemu_get_be64(f);
  220. }
  221. }
  222. if (c->post_load) {
  223. c->post_load(pit);
  224. }
  225. return 0;
  226. }
  227. static void pit_dispatch_pre_save(void *opaque)
  228. {
  229. PITCommonState *s = opaque;
  230. PITCommonClass *c = PIT_COMMON_GET_CLASS(s);
  231. if (c->pre_save) {
  232. c->pre_save(s);
  233. }
  234. }
  235. static int pit_dispatch_post_load(void *opaque, int version_id)
  236. {
  237. PITCommonState *s = opaque;
  238. PITCommonClass *c = PIT_COMMON_GET_CLASS(s);
  239. if (c->post_load) {
  240. c->post_load(s);
  241. }
  242. return 0;
  243. }
  244. static const VMStateDescription vmstate_pit_common = {
  245. .name = "i8254",
  246. .version_id = 3,
  247. .minimum_version_id = 2,
  248. .minimum_version_id_old = 1,
  249. .load_state_old = pit_load_old,
  250. .pre_save = pit_dispatch_pre_save,
  251. .post_load = pit_dispatch_post_load,
  252. .fields = (VMStateField[]) {
  253. VMSTATE_UINT32_V(channels[0].irq_disabled, PITCommonState, 3),
  254. VMSTATE_STRUCT_ARRAY(channels, PITCommonState, 3, 2,
  255. vmstate_pit_channel, PITChannelState),
  256. VMSTATE_INT64(channels[0].next_transition_time,
  257. PITCommonState), /* formerly irq_timer */
  258. VMSTATE_END_OF_LIST()
  259. }
  260. };
  261. static void pit_common_class_init(ObjectClass *klass, void *data)
  262. {
  263. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  264. DeviceClass *dc = DEVICE_CLASS(klass);
  265. ic->init = pit_init_common;
  266. dc->vmsd = &vmstate_pit_common;
  267. dc->no_user = 1;
  268. }
  269. static const TypeInfo pit_common_type = {
  270. .name = TYPE_PIT_COMMON,
  271. .parent = TYPE_ISA_DEVICE,
  272. .instance_size = sizeof(PITCommonState),
  273. .class_size = sizeof(PITCommonClass),
  274. .class_init = pit_common_class_init,
  275. .abstract = true,
  276. };
  277. static void register_devices(void)
  278. {
  279. type_register_static(&pit_common_type);
  280. }
  281. type_init(register_devices);