i8254.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * 8253/8254 interval timer emulation
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2006 Intel Corporation
  6. * Copyright (c) 2007 Keir Fraser, XenSource Inc
  7. * Copyright (c) 2008 Intel Corporation
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. * Authors:
  28. * Sheng Yang <sheng.yang@intel.com>
  29. * Based on QEMU and Xen.
  30. */
  31. #include <linux/kvm_host.h>
  32. #include "irq.h"
  33. #include "i8254.h"
  34. #ifndef CONFIG_X86_64
  35. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  36. #else
  37. #define mod_64(x, y) ((x) % (y))
  38. #endif
  39. #define RW_STATE_LSB 1
  40. #define RW_STATE_MSB 2
  41. #define RW_STATE_WORD0 3
  42. #define RW_STATE_WORD1 4
  43. /* Compute with 96 bit intermediate result: (a*b)/c */
  44. static u64 muldiv64(u64 a, u32 b, u32 c)
  45. {
  46. union {
  47. u64 ll;
  48. struct {
  49. u32 low, high;
  50. } l;
  51. } u, res;
  52. u64 rl, rh;
  53. u.ll = a;
  54. rl = (u64)u.l.low * (u64)b;
  55. rh = (u64)u.l.high * (u64)b;
  56. rh += (rl >> 32);
  57. res.l.high = div64_u64(rh, c);
  58. res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
  59. return res.ll;
  60. }
  61. static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
  62. {
  63. struct kvm_kpit_channel_state *c =
  64. &kvm->arch.vpit->pit_state.channels[channel];
  65. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  66. switch (c->mode) {
  67. default:
  68. case 0:
  69. case 4:
  70. /* XXX: just disable/enable counting */
  71. break;
  72. case 1:
  73. case 2:
  74. case 3:
  75. case 5:
  76. /* Restart counting on rising edge. */
  77. if (c->gate < val)
  78. c->count_load_time = ktime_get();
  79. break;
  80. }
  81. c->gate = val;
  82. }
  83. static int pit_get_gate(struct kvm *kvm, int channel)
  84. {
  85. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  86. return kvm->arch.vpit->pit_state.channels[channel].gate;
  87. }
  88. static s64 __kpit_elapsed(struct kvm *kvm)
  89. {
  90. s64 elapsed;
  91. ktime_t remaining;
  92. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  93. if (!ps->pit_timer.period)
  94. return 0;
  95. /*
  96. * The Counter does not stop when it reaches zero. In
  97. * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
  98. * the highest count, either FFFF hex for binary counting
  99. * or 9999 for BCD counting, and continues counting.
  100. * Modes 2 and 3 are periodic; the Counter reloads
  101. * itself with the initial count and continues counting
  102. * from there.
  103. */
  104. remaining = hrtimer_expires_remaining(&ps->pit_timer.timer);
  105. elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
  106. elapsed = mod_64(elapsed, ps->pit_timer.period);
  107. return elapsed;
  108. }
  109. static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c,
  110. int channel)
  111. {
  112. if (channel == 0)
  113. return __kpit_elapsed(kvm);
  114. return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
  115. }
  116. static int pit_get_count(struct kvm *kvm, int channel)
  117. {
  118. struct kvm_kpit_channel_state *c =
  119. &kvm->arch.vpit->pit_state.channels[channel];
  120. s64 d, t;
  121. int counter;
  122. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  123. t = kpit_elapsed(kvm, c, channel);
  124. d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
  125. switch (c->mode) {
  126. case 0:
  127. case 1:
  128. case 4:
  129. case 5:
  130. counter = (c->count - d) & 0xffff;
  131. break;
  132. case 3:
  133. /* XXX: may be incorrect for odd counts */
  134. counter = c->count - (mod_64((2 * d), c->count));
  135. break;
  136. default:
  137. counter = c->count - mod_64(d, c->count);
  138. break;
  139. }
  140. return counter;
  141. }
  142. static int pit_get_out(struct kvm *kvm, int channel)
  143. {
  144. struct kvm_kpit_channel_state *c =
  145. &kvm->arch.vpit->pit_state.channels[channel];
  146. s64 d, t;
  147. int out;
  148. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  149. t = kpit_elapsed(kvm, c, channel);
  150. d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
  151. switch (c->mode) {
  152. default:
  153. case 0:
  154. out = (d >= c->count);
  155. break;
  156. case 1:
  157. out = (d < c->count);
  158. break;
  159. case 2:
  160. out = ((mod_64(d, c->count) == 0) && (d != 0));
  161. break;
  162. case 3:
  163. out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
  164. break;
  165. case 4:
  166. case 5:
  167. out = (d == c->count);
  168. break;
  169. }
  170. return out;
  171. }
  172. static void pit_latch_count(struct kvm *kvm, int channel)
  173. {
  174. struct kvm_kpit_channel_state *c =
  175. &kvm->arch.vpit->pit_state.channels[channel];
  176. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  177. if (!c->count_latched) {
  178. c->latched_count = pit_get_count(kvm, channel);
  179. c->count_latched = c->rw_mode;
  180. }
  181. }
  182. static void pit_latch_status(struct kvm *kvm, int channel)
  183. {
  184. struct kvm_kpit_channel_state *c =
  185. &kvm->arch.vpit->pit_state.channels[channel];
  186. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  187. if (!c->status_latched) {
  188. /* TODO: Return NULL COUNT (bit 6). */
  189. c->status = ((pit_get_out(kvm, channel) << 7) |
  190. (c->rw_mode << 4) |
  191. (c->mode << 1) |
  192. c->bcd);
  193. c->status_latched = 1;
  194. }
  195. }
  196. int pit_has_pending_timer(struct kvm_vcpu *vcpu)
  197. {
  198. struct kvm_pit *pit = vcpu->kvm->arch.vpit;
  199. if (pit && kvm_vcpu_is_bsp(vcpu) && pit->pit_state.irq_ack)
  200. return atomic_read(&pit->pit_state.pit_timer.pending);
  201. return 0;
  202. }
  203. static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
  204. {
  205. struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
  206. irq_ack_notifier);
  207. spin_lock(&ps->inject_lock);
  208. if (atomic_dec_return(&ps->pit_timer.pending) < 0)
  209. atomic_inc(&ps->pit_timer.pending);
  210. ps->irq_ack = 1;
  211. spin_unlock(&ps->inject_lock);
  212. }
  213. void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
  214. {
  215. struct kvm_pit *pit = vcpu->kvm->arch.vpit;
  216. struct hrtimer *timer;
  217. if (!kvm_vcpu_is_bsp(vcpu) || !pit)
  218. return;
  219. timer = &pit->pit_state.pit_timer.timer;
  220. if (hrtimer_cancel(timer))
  221. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  222. }
  223. static void destroy_pit_timer(struct kvm_timer *pt)
  224. {
  225. pr_debug("pit: execute del timer!\n");
  226. hrtimer_cancel(&pt->timer);
  227. }
  228. static bool kpit_is_periodic(struct kvm_timer *ktimer)
  229. {
  230. struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
  231. pit_timer);
  232. return ps->is_periodic;
  233. }
  234. static struct kvm_timer_ops kpit_ops = {
  235. .is_periodic = kpit_is_periodic,
  236. };
  237. static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
  238. {
  239. struct kvm_timer *pt = &ps->pit_timer;
  240. s64 interval;
  241. interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
  242. pr_debug("pit: create pit timer, interval is %llu nsec\n", interval);
  243. /* TODO The new value only affected after the retriggered */
  244. hrtimer_cancel(&pt->timer);
  245. pt->period = interval;
  246. ps->is_periodic = is_period;
  247. pt->timer.function = kvm_timer_fn;
  248. pt->t_ops = &kpit_ops;
  249. pt->kvm = ps->pit->kvm;
  250. pt->vcpu = pt->kvm->bsp_vcpu;
  251. atomic_set(&pt->pending, 0);
  252. ps->irq_ack = 1;
  253. hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
  254. HRTIMER_MODE_ABS);
  255. }
  256. static void pit_load_count(struct kvm *kvm, int channel, u32 val)
  257. {
  258. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  259. WARN_ON(!mutex_is_locked(&ps->lock));
  260. pr_debug("pit: load_count val is %d, channel is %d\n", val, channel);
  261. /*
  262. * The largest possible initial count is 0; this is equivalent
  263. * to 216 for binary counting and 104 for BCD counting.
  264. */
  265. if (val == 0)
  266. val = 0x10000;
  267. ps->channels[channel].count = val;
  268. if (channel != 0) {
  269. ps->channels[channel].count_load_time = ktime_get();
  270. return;
  271. }
  272. /* Two types of timer
  273. * mode 1 is one shot, mode 2 is period, otherwise del timer */
  274. switch (ps->channels[0].mode) {
  275. case 0:
  276. case 1:
  277. /* FIXME: enhance mode 4 precision */
  278. case 4:
  279. create_pit_timer(ps, val, 0);
  280. break;
  281. case 2:
  282. case 3:
  283. create_pit_timer(ps, val, 1);
  284. break;
  285. default:
  286. destroy_pit_timer(&ps->pit_timer);
  287. }
  288. }
  289. void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val)
  290. {
  291. pit_load_count(kvm, channel, val);
  292. }
  293. static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
  294. {
  295. return container_of(dev, struct kvm_pit, dev);
  296. }
  297. static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
  298. {
  299. return container_of(dev, struct kvm_pit, speaker_dev);
  300. }
  301. static inline int pit_in_range(gpa_t addr)
  302. {
  303. return ((addr >= KVM_PIT_BASE_ADDRESS) &&
  304. (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
  305. }
  306. static int pit_ioport_write(struct kvm_io_device *this,
  307. gpa_t addr, int len, const void *data)
  308. {
  309. struct kvm_pit *pit = dev_to_pit(this);
  310. struct kvm_kpit_state *pit_state = &pit->pit_state;
  311. struct kvm *kvm = pit->kvm;
  312. int channel, access;
  313. struct kvm_kpit_channel_state *s;
  314. u32 val = *(u32 *) data;
  315. if (!pit_in_range(addr))
  316. return -EOPNOTSUPP;
  317. val &= 0xff;
  318. addr &= KVM_PIT_CHANNEL_MASK;
  319. mutex_lock(&pit_state->lock);
  320. if (val != 0)
  321. pr_debug("pit: write addr is 0x%x, len is %d, val is 0x%x\n",
  322. (unsigned int)addr, len, val);
  323. if (addr == 3) {
  324. channel = val >> 6;
  325. if (channel == 3) {
  326. /* Read-Back Command. */
  327. for (channel = 0; channel < 3; channel++) {
  328. s = &pit_state->channels[channel];
  329. if (val & (2 << channel)) {
  330. if (!(val & 0x20))
  331. pit_latch_count(kvm, channel);
  332. if (!(val & 0x10))
  333. pit_latch_status(kvm, channel);
  334. }
  335. }
  336. } else {
  337. /* Select Counter <channel>. */
  338. s = &pit_state->channels[channel];
  339. access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
  340. if (access == 0) {
  341. pit_latch_count(kvm, channel);
  342. } else {
  343. s->rw_mode = access;
  344. s->read_state = access;
  345. s->write_state = access;
  346. s->mode = (val >> 1) & 7;
  347. if (s->mode > 5)
  348. s->mode -= 4;
  349. s->bcd = val & 1;
  350. }
  351. }
  352. } else {
  353. /* Write Count. */
  354. s = &pit_state->channels[addr];
  355. switch (s->write_state) {
  356. default:
  357. case RW_STATE_LSB:
  358. pit_load_count(kvm, addr, val);
  359. break;
  360. case RW_STATE_MSB:
  361. pit_load_count(kvm, addr, val << 8);
  362. break;
  363. case RW_STATE_WORD0:
  364. s->write_latch = val;
  365. s->write_state = RW_STATE_WORD1;
  366. break;
  367. case RW_STATE_WORD1:
  368. pit_load_count(kvm, addr, s->write_latch | (val << 8));
  369. s->write_state = RW_STATE_WORD0;
  370. break;
  371. }
  372. }
  373. mutex_unlock(&pit_state->lock);
  374. return 0;
  375. }
  376. static int pit_ioport_read(struct kvm_io_device *this,
  377. gpa_t addr, int len, void *data)
  378. {
  379. struct kvm_pit *pit = dev_to_pit(this);
  380. struct kvm_kpit_state *pit_state = &pit->pit_state;
  381. struct kvm *kvm = pit->kvm;
  382. int ret, count;
  383. struct kvm_kpit_channel_state *s;
  384. if (!pit_in_range(addr))
  385. return -EOPNOTSUPP;
  386. addr &= KVM_PIT_CHANNEL_MASK;
  387. s = &pit_state->channels[addr];
  388. mutex_lock(&pit_state->lock);
  389. if (s->status_latched) {
  390. s->status_latched = 0;
  391. ret = s->status;
  392. } else if (s->count_latched) {
  393. switch (s->count_latched) {
  394. default:
  395. case RW_STATE_LSB:
  396. ret = s->latched_count & 0xff;
  397. s->count_latched = 0;
  398. break;
  399. case RW_STATE_MSB:
  400. ret = s->latched_count >> 8;
  401. s->count_latched = 0;
  402. break;
  403. case RW_STATE_WORD0:
  404. ret = s->latched_count & 0xff;
  405. s->count_latched = RW_STATE_MSB;
  406. break;
  407. }
  408. } else {
  409. switch (s->read_state) {
  410. default:
  411. case RW_STATE_LSB:
  412. count = pit_get_count(kvm, addr);
  413. ret = count & 0xff;
  414. break;
  415. case RW_STATE_MSB:
  416. count = pit_get_count(kvm, addr);
  417. ret = (count >> 8) & 0xff;
  418. break;
  419. case RW_STATE_WORD0:
  420. count = pit_get_count(kvm, addr);
  421. ret = count & 0xff;
  422. s->read_state = RW_STATE_WORD1;
  423. break;
  424. case RW_STATE_WORD1:
  425. count = pit_get_count(kvm, addr);
  426. ret = (count >> 8) & 0xff;
  427. s->read_state = RW_STATE_WORD0;
  428. break;
  429. }
  430. }
  431. if (len > sizeof(ret))
  432. len = sizeof(ret);
  433. memcpy(data, (char *)&ret, len);
  434. mutex_unlock(&pit_state->lock);
  435. return 0;
  436. }
  437. static int speaker_ioport_write(struct kvm_io_device *this,
  438. gpa_t addr, int len, const void *data)
  439. {
  440. struct kvm_pit *pit = speaker_to_pit(this);
  441. struct kvm_kpit_state *pit_state = &pit->pit_state;
  442. struct kvm *kvm = pit->kvm;
  443. u32 val = *(u32 *) data;
  444. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  445. return -EOPNOTSUPP;
  446. mutex_lock(&pit_state->lock);
  447. pit_state->speaker_data_on = (val >> 1) & 1;
  448. pit_set_gate(kvm, 2, val & 1);
  449. mutex_unlock(&pit_state->lock);
  450. return 0;
  451. }
  452. static int speaker_ioport_read(struct kvm_io_device *this,
  453. gpa_t addr, int len, void *data)
  454. {
  455. struct kvm_pit *pit = speaker_to_pit(this);
  456. struct kvm_kpit_state *pit_state = &pit->pit_state;
  457. struct kvm *kvm = pit->kvm;
  458. unsigned int refresh_clock;
  459. int ret;
  460. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  461. return -EOPNOTSUPP;
  462. /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
  463. refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
  464. mutex_lock(&pit_state->lock);
  465. ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
  466. (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
  467. if (len > sizeof(ret))
  468. len = sizeof(ret);
  469. memcpy(data, (char *)&ret, len);
  470. mutex_unlock(&pit_state->lock);
  471. return 0;
  472. }
  473. void kvm_pit_reset(struct kvm_pit *pit)
  474. {
  475. int i;
  476. struct kvm_kpit_channel_state *c;
  477. mutex_lock(&pit->pit_state.lock);
  478. for (i = 0; i < 3; i++) {
  479. c = &pit->pit_state.channels[i];
  480. c->mode = 0xff;
  481. c->gate = (i != 2);
  482. pit_load_count(pit->kvm, i, 0);
  483. }
  484. mutex_unlock(&pit->pit_state.lock);
  485. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  486. pit->pit_state.irq_ack = 1;
  487. }
  488. static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
  489. {
  490. struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
  491. if (!mask) {
  492. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  493. pit->pit_state.irq_ack = 1;
  494. }
  495. }
  496. static const struct kvm_io_device_ops pit_dev_ops = {
  497. .read = pit_ioport_read,
  498. .write = pit_ioport_write,
  499. };
  500. static const struct kvm_io_device_ops speaker_dev_ops = {
  501. .read = speaker_ioport_read,
  502. .write = speaker_ioport_write,
  503. };
  504. /* Caller must have writers lock on slots_lock */
  505. struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
  506. {
  507. struct kvm_pit *pit;
  508. struct kvm_kpit_state *pit_state;
  509. pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
  510. if (!pit)
  511. return NULL;
  512. pit->irq_source_id = kvm_request_irq_source_id(kvm);
  513. if (pit->irq_source_id < 0) {
  514. kfree(pit);
  515. return NULL;
  516. }
  517. mutex_init(&pit->pit_state.lock);
  518. mutex_lock(&pit->pit_state.lock);
  519. spin_lock_init(&pit->pit_state.inject_lock);
  520. kvm->arch.vpit = pit;
  521. pit->kvm = kvm;
  522. pit_state = &pit->pit_state;
  523. pit_state->pit = pit;
  524. hrtimer_init(&pit_state->pit_timer.timer,
  525. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  526. pit_state->irq_ack_notifier.gsi = 0;
  527. pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
  528. kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
  529. pit_state->pit_timer.reinject = true;
  530. mutex_unlock(&pit->pit_state.lock);
  531. kvm_pit_reset(pit);
  532. pit->mask_notifier.func = pit_mask_notifer;
  533. kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  534. kvm_iodevice_init(&pit->dev, &pit_dev_ops);
  535. __kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
  536. if (flags & KVM_PIT_SPEAKER_DUMMY) {
  537. kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
  538. __kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
  539. }
  540. return pit;
  541. }
  542. void kvm_free_pit(struct kvm *kvm)
  543. {
  544. struct hrtimer *timer;
  545. if (kvm->arch.vpit) {
  546. kvm_unregister_irq_mask_notifier(kvm, 0,
  547. &kvm->arch.vpit->mask_notifier);
  548. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  549. timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
  550. hrtimer_cancel(timer);
  551. kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
  552. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  553. kfree(kvm->arch.vpit);
  554. }
  555. }
  556. static void __inject_pit_timer_intr(struct kvm *kvm)
  557. {
  558. struct kvm_vcpu *vcpu;
  559. int i;
  560. mutex_lock(&kvm->irq_lock);
  561. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
  562. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
  563. mutex_unlock(&kvm->irq_lock);
  564. /*
  565. * Provides NMI watchdog support via Virtual Wire mode.
  566. * The route is: PIT -> PIC -> LVT0 in NMI mode.
  567. *
  568. * Note: Our Virtual Wire implementation is simplified, only
  569. * propagating PIT interrupts to all VCPUs when they have set
  570. * LVT0 to NMI delivery. Other PIC interrupts are just sent to
  571. * VCPU0, and only if its LVT0 is in EXTINT mode.
  572. */
  573. if (kvm->arch.vapics_in_nmi_mode > 0)
  574. kvm_for_each_vcpu(i, vcpu, kvm)
  575. kvm_apic_nmi_wd_deliver(vcpu);
  576. }
  577. void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
  578. {
  579. struct kvm_pit *pit = vcpu->kvm->arch.vpit;
  580. struct kvm *kvm = vcpu->kvm;
  581. struct kvm_kpit_state *ps;
  582. if (vcpu && pit) {
  583. int inject = 0;
  584. ps = &pit->pit_state;
  585. /* Try to inject pending interrupts when
  586. * last one has been acked.
  587. */
  588. spin_lock(&ps->inject_lock);
  589. if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) {
  590. ps->irq_ack = 0;
  591. inject = 1;
  592. }
  593. spin_unlock(&ps->inject_lock);
  594. if (inject)
  595. __inject_pit_timer_intr(kvm);
  596. }
  597. }