i8254.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. remaining = hrtimer_expires_remaining(&ps->pit_timer.timer);
  94. if (ktime_to_ns(remaining) < 0)
  95. remaining = ktime_set(0, 0);
  96. elapsed = ps->pit_timer.period;
  97. if (ktime_to_ns(remaining) <= ps->pit_timer.period)
  98. elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
  99. return elapsed;
  100. }
  101. static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c,
  102. int channel)
  103. {
  104. if (channel == 0)
  105. return __kpit_elapsed(kvm);
  106. return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time));
  107. }
  108. static int pit_get_count(struct kvm *kvm, int channel)
  109. {
  110. struct kvm_kpit_channel_state *c =
  111. &kvm->arch.vpit->pit_state.channels[channel];
  112. s64 d, t;
  113. int counter;
  114. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  115. t = kpit_elapsed(kvm, c, channel);
  116. d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
  117. switch (c->mode) {
  118. case 0:
  119. case 1:
  120. case 4:
  121. case 5:
  122. counter = (c->count - d) & 0xffff;
  123. break;
  124. case 3:
  125. /* XXX: may be incorrect for odd counts */
  126. counter = c->count - (mod_64((2 * d), c->count));
  127. break;
  128. default:
  129. counter = c->count - mod_64(d, c->count);
  130. break;
  131. }
  132. return counter;
  133. }
  134. static int pit_get_out(struct kvm *kvm, int channel)
  135. {
  136. struct kvm_kpit_channel_state *c =
  137. &kvm->arch.vpit->pit_state.channels[channel];
  138. s64 d, t;
  139. int out;
  140. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  141. t = kpit_elapsed(kvm, c, channel);
  142. d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC);
  143. switch (c->mode) {
  144. default:
  145. case 0:
  146. out = (d >= c->count);
  147. break;
  148. case 1:
  149. out = (d < c->count);
  150. break;
  151. case 2:
  152. out = ((mod_64(d, c->count) == 0) && (d != 0));
  153. break;
  154. case 3:
  155. out = (mod_64(d, c->count) < ((c->count + 1) >> 1));
  156. break;
  157. case 4:
  158. case 5:
  159. out = (d == c->count);
  160. break;
  161. }
  162. return out;
  163. }
  164. static void pit_latch_count(struct kvm *kvm, int channel)
  165. {
  166. struct kvm_kpit_channel_state *c =
  167. &kvm->arch.vpit->pit_state.channels[channel];
  168. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  169. if (!c->count_latched) {
  170. c->latched_count = pit_get_count(kvm, channel);
  171. c->count_latched = c->rw_mode;
  172. }
  173. }
  174. static void pit_latch_status(struct kvm *kvm, int channel)
  175. {
  176. struct kvm_kpit_channel_state *c =
  177. &kvm->arch.vpit->pit_state.channels[channel];
  178. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  179. if (!c->status_latched) {
  180. /* TODO: Return NULL COUNT (bit 6). */
  181. c->status = ((pit_get_out(kvm, channel) << 7) |
  182. (c->rw_mode << 4) |
  183. (c->mode << 1) |
  184. c->bcd);
  185. c->status_latched = 1;
  186. }
  187. }
  188. int pit_has_pending_timer(struct kvm_vcpu *vcpu)
  189. {
  190. struct kvm_pit *pit = vcpu->kvm->arch.vpit;
  191. if (pit && vcpu->vcpu_id == 0 && pit->pit_state.irq_ack)
  192. return atomic_read(&pit->pit_state.pit_timer.pending);
  193. return 0;
  194. }
  195. static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
  196. {
  197. struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
  198. irq_ack_notifier);
  199. spin_lock(&ps->inject_lock);
  200. if (atomic_dec_return(&ps->pit_timer.pending) < 0)
  201. atomic_inc(&ps->pit_timer.pending);
  202. ps->irq_ack = 1;
  203. spin_unlock(&ps->inject_lock);
  204. }
  205. void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
  206. {
  207. struct kvm_pit *pit = vcpu->kvm->arch.vpit;
  208. struct hrtimer *timer;
  209. if (vcpu->vcpu_id != 0 || !pit)
  210. return;
  211. timer = &pit->pit_state.pit_timer.timer;
  212. if (hrtimer_cancel(timer))
  213. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  214. }
  215. static void destroy_pit_timer(struct kvm_timer *pt)
  216. {
  217. pr_debug("pit: execute del timer!\n");
  218. hrtimer_cancel(&pt->timer);
  219. }
  220. static bool kpit_is_periodic(struct kvm_timer *ktimer)
  221. {
  222. struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
  223. pit_timer);
  224. return ps->is_periodic;
  225. }
  226. struct kvm_timer_ops kpit_ops = {
  227. .is_periodic = kpit_is_periodic,
  228. };
  229. static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
  230. {
  231. struct kvm_timer *pt = &ps->pit_timer;
  232. s64 interval;
  233. interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
  234. pr_debug("pit: create pit timer, interval is %llu nsec\n", interval);
  235. /* TODO The new value only affected after the retriggered */
  236. hrtimer_cancel(&pt->timer);
  237. pt->period = (is_period == 0) ? 0 : interval;
  238. ps->is_periodic = is_period;
  239. pt->timer.function = kvm_timer_fn;
  240. pt->t_ops = &kpit_ops;
  241. pt->kvm = ps->pit->kvm;
  242. pt->vcpu_id = 0;
  243. atomic_set(&pt->pending, 0);
  244. ps->irq_ack = 1;
  245. hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
  246. HRTIMER_MODE_ABS);
  247. }
  248. static void pit_load_count(struct kvm *kvm, int channel, u32 val)
  249. {
  250. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  251. WARN_ON(!mutex_is_locked(&ps->lock));
  252. pr_debug("pit: load_count val is %d, channel is %d\n", val, channel);
  253. /*
  254. * Though spec said the state of 8254 is undefined after power-up,
  255. * seems some tricky OS like Windows XP depends on IRQ0 interrupt
  256. * when booting up.
  257. * So here setting initialize rate for it, and not a specific number
  258. */
  259. if (val == 0)
  260. val = 0x10000;
  261. ps->channels[channel].count = val;
  262. if (channel != 0) {
  263. ps->channels[channel].count_load_time = ktime_get();
  264. return;
  265. }
  266. /* Two types of timer
  267. * mode 1 is one shot, mode 2 is period, otherwise del timer */
  268. switch (ps->channels[0].mode) {
  269. case 1:
  270. /* FIXME: enhance mode 4 precision */
  271. case 4:
  272. create_pit_timer(ps, val, 0);
  273. break;
  274. case 2:
  275. case 3:
  276. create_pit_timer(ps, val, 1);
  277. break;
  278. default:
  279. destroy_pit_timer(&ps->pit_timer);
  280. }
  281. }
  282. void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val)
  283. {
  284. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  285. pit_load_count(kvm, channel, val);
  286. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  287. }
  288. static void pit_ioport_write(struct kvm_io_device *this,
  289. gpa_t addr, int len, const void *data)
  290. {
  291. struct kvm_pit *pit = (struct kvm_pit *)this->private;
  292. struct kvm_kpit_state *pit_state = &pit->pit_state;
  293. struct kvm *kvm = pit->kvm;
  294. int channel, access;
  295. struct kvm_kpit_channel_state *s;
  296. u32 val = *(u32 *) data;
  297. val &= 0xff;
  298. addr &= KVM_PIT_CHANNEL_MASK;
  299. mutex_lock(&pit_state->lock);
  300. if (val != 0)
  301. pr_debug("pit: write addr is 0x%x, len is %d, val is 0x%x\n",
  302. (unsigned int)addr, len, val);
  303. if (addr == 3) {
  304. channel = val >> 6;
  305. if (channel == 3) {
  306. /* Read-Back Command. */
  307. for (channel = 0; channel < 3; channel++) {
  308. s = &pit_state->channels[channel];
  309. if (val & (2 << channel)) {
  310. if (!(val & 0x20))
  311. pit_latch_count(kvm, channel);
  312. if (!(val & 0x10))
  313. pit_latch_status(kvm, channel);
  314. }
  315. }
  316. } else {
  317. /* Select Counter <channel>. */
  318. s = &pit_state->channels[channel];
  319. access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
  320. if (access == 0) {
  321. pit_latch_count(kvm, channel);
  322. } else {
  323. s->rw_mode = access;
  324. s->read_state = access;
  325. s->write_state = access;
  326. s->mode = (val >> 1) & 7;
  327. if (s->mode > 5)
  328. s->mode -= 4;
  329. s->bcd = val & 1;
  330. }
  331. }
  332. } else {
  333. /* Write Count. */
  334. s = &pit_state->channels[addr];
  335. switch (s->write_state) {
  336. default:
  337. case RW_STATE_LSB:
  338. pit_load_count(kvm, addr, val);
  339. break;
  340. case RW_STATE_MSB:
  341. pit_load_count(kvm, addr, val << 8);
  342. break;
  343. case RW_STATE_WORD0:
  344. s->write_latch = val;
  345. s->write_state = RW_STATE_WORD1;
  346. break;
  347. case RW_STATE_WORD1:
  348. pit_load_count(kvm, addr, s->write_latch | (val << 8));
  349. s->write_state = RW_STATE_WORD0;
  350. break;
  351. }
  352. }
  353. mutex_unlock(&pit_state->lock);
  354. }
  355. static void pit_ioport_read(struct kvm_io_device *this,
  356. gpa_t addr, int len, void *data)
  357. {
  358. struct kvm_pit *pit = (struct kvm_pit *)this->private;
  359. struct kvm_kpit_state *pit_state = &pit->pit_state;
  360. struct kvm *kvm = pit->kvm;
  361. int ret, count;
  362. struct kvm_kpit_channel_state *s;
  363. addr &= KVM_PIT_CHANNEL_MASK;
  364. s = &pit_state->channels[addr];
  365. mutex_lock(&pit_state->lock);
  366. if (s->status_latched) {
  367. s->status_latched = 0;
  368. ret = s->status;
  369. } else if (s->count_latched) {
  370. switch (s->count_latched) {
  371. default:
  372. case RW_STATE_LSB:
  373. ret = s->latched_count & 0xff;
  374. s->count_latched = 0;
  375. break;
  376. case RW_STATE_MSB:
  377. ret = s->latched_count >> 8;
  378. s->count_latched = 0;
  379. break;
  380. case RW_STATE_WORD0:
  381. ret = s->latched_count & 0xff;
  382. s->count_latched = RW_STATE_MSB;
  383. break;
  384. }
  385. } else {
  386. switch (s->read_state) {
  387. default:
  388. case RW_STATE_LSB:
  389. count = pit_get_count(kvm, addr);
  390. ret = count & 0xff;
  391. break;
  392. case RW_STATE_MSB:
  393. count = pit_get_count(kvm, addr);
  394. ret = (count >> 8) & 0xff;
  395. break;
  396. case RW_STATE_WORD0:
  397. count = pit_get_count(kvm, addr);
  398. ret = count & 0xff;
  399. s->read_state = RW_STATE_WORD1;
  400. break;
  401. case RW_STATE_WORD1:
  402. count = pit_get_count(kvm, addr);
  403. ret = (count >> 8) & 0xff;
  404. s->read_state = RW_STATE_WORD0;
  405. break;
  406. }
  407. }
  408. if (len > sizeof(ret))
  409. len = sizeof(ret);
  410. memcpy(data, (char *)&ret, len);
  411. mutex_unlock(&pit_state->lock);
  412. }
  413. static int pit_in_range(struct kvm_io_device *this, gpa_t addr,
  414. int len, int is_write)
  415. {
  416. return ((addr >= KVM_PIT_BASE_ADDRESS) &&
  417. (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
  418. }
  419. static void speaker_ioport_write(struct kvm_io_device *this,
  420. gpa_t addr, int len, const void *data)
  421. {
  422. struct kvm_pit *pit = (struct kvm_pit *)this->private;
  423. struct kvm_kpit_state *pit_state = &pit->pit_state;
  424. struct kvm *kvm = pit->kvm;
  425. u32 val = *(u32 *) data;
  426. mutex_lock(&pit_state->lock);
  427. pit_state->speaker_data_on = (val >> 1) & 1;
  428. pit_set_gate(kvm, 2, val & 1);
  429. mutex_unlock(&pit_state->lock);
  430. }
  431. static void speaker_ioport_read(struct kvm_io_device *this,
  432. gpa_t addr, int len, void *data)
  433. {
  434. struct kvm_pit *pit = (struct kvm_pit *)this->private;
  435. struct kvm_kpit_state *pit_state = &pit->pit_state;
  436. struct kvm *kvm = pit->kvm;
  437. unsigned int refresh_clock;
  438. int ret;
  439. /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
  440. refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
  441. mutex_lock(&pit_state->lock);
  442. ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
  443. (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
  444. if (len > sizeof(ret))
  445. len = sizeof(ret);
  446. memcpy(data, (char *)&ret, len);
  447. mutex_unlock(&pit_state->lock);
  448. }
  449. static int speaker_in_range(struct kvm_io_device *this, gpa_t addr,
  450. int len, int is_write)
  451. {
  452. return (addr == KVM_SPEAKER_BASE_ADDRESS);
  453. }
  454. void kvm_pit_reset(struct kvm_pit *pit)
  455. {
  456. int i;
  457. struct kvm_kpit_channel_state *c;
  458. mutex_lock(&pit->pit_state.lock);
  459. for (i = 0; i < 3; i++) {
  460. c = &pit->pit_state.channels[i];
  461. c->mode = 0xff;
  462. c->gate = (i != 2);
  463. pit_load_count(pit->kvm, i, 0);
  464. }
  465. mutex_unlock(&pit->pit_state.lock);
  466. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  467. pit->pit_state.irq_ack = 1;
  468. }
  469. static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
  470. {
  471. struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
  472. if (!mask) {
  473. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  474. pit->pit_state.irq_ack = 1;
  475. }
  476. }
  477. struct kvm_pit *kvm_create_pit(struct kvm *kvm)
  478. {
  479. struct kvm_pit *pit;
  480. struct kvm_kpit_state *pit_state;
  481. pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
  482. if (!pit)
  483. return NULL;
  484. pit->irq_source_id = kvm_request_irq_source_id(kvm);
  485. if (pit->irq_source_id < 0) {
  486. kfree(pit);
  487. return NULL;
  488. }
  489. mutex_init(&pit->pit_state.lock);
  490. mutex_lock(&pit->pit_state.lock);
  491. spin_lock_init(&pit->pit_state.inject_lock);
  492. /* Initialize PIO device */
  493. pit->dev.read = pit_ioport_read;
  494. pit->dev.write = pit_ioport_write;
  495. pit->dev.in_range = pit_in_range;
  496. pit->dev.private = pit;
  497. kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
  498. pit->speaker_dev.read = speaker_ioport_read;
  499. pit->speaker_dev.write = speaker_ioport_write;
  500. pit->speaker_dev.in_range = speaker_in_range;
  501. pit->speaker_dev.private = pit;
  502. kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
  503. kvm->arch.vpit = pit;
  504. pit->kvm = kvm;
  505. pit_state = &pit->pit_state;
  506. pit_state->pit = pit;
  507. hrtimer_init(&pit_state->pit_timer.timer,
  508. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  509. pit_state->irq_ack_notifier.gsi = 0;
  510. pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
  511. kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
  512. pit_state->pit_timer.reinject = true;
  513. mutex_unlock(&pit->pit_state.lock);
  514. kvm_pit_reset(pit);
  515. pit->mask_notifier.func = pit_mask_notifer;
  516. kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  517. return pit;
  518. }
  519. void kvm_free_pit(struct kvm *kvm)
  520. {
  521. struct hrtimer *timer;
  522. if (kvm->arch.vpit) {
  523. kvm_unregister_irq_mask_notifier(kvm, 0,
  524. &kvm->arch.vpit->mask_notifier);
  525. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  526. timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
  527. hrtimer_cancel(timer);
  528. kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
  529. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  530. kfree(kvm->arch.vpit);
  531. }
  532. }
  533. static void __inject_pit_timer_intr(struct kvm *kvm)
  534. {
  535. struct kvm_vcpu *vcpu;
  536. int i;
  537. mutex_lock(&kvm->lock);
  538. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
  539. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
  540. mutex_unlock(&kvm->lock);
  541. /*
  542. * Provides NMI watchdog support via Virtual Wire mode.
  543. * The route is: PIT -> PIC -> LVT0 in NMI mode.
  544. *
  545. * Note: Our Virtual Wire implementation is simplified, only
  546. * propagating PIT interrupts to all VCPUs when they have set
  547. * LVT0 to NMI delivery. Other PIC interrupts are just sent to
  548. * VCPU0, and only if its LVT0 is in EXTINT mode.
  549. */
  550. if (kvm->arch.vapics_in_nmi_mode > 0)
  551. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  552. vcpu = kvm->vcpus[i];
  553. if (vcpu)
  554. kvm_apic_nmi_wd_deliver(vcpu);
  555. }
  556. }
  557. void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
  558. {
  559. struct kvm_pit *pit = vcpu->kvm->arch.vpit;
  560. struct kvm *kvm = vcpu->kvm;
  561. struct kvm_kpit_state *ps;
  562. if (vcpu && pit) {
  563. int inject = 0;
  564. ps = &pit->pit_state;
  565. /* Try to inject pending interrupts when
  566. * last one has been acked.
  567. */
  568. spin_lock(&ps->inject_lock);
  569. if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) {
  570. ps->irq_ack = 0;
  571. inject = 1;
  572. }
  573. spin_unlock(&ps->inject_lock);
  574. if (inject)
  575. __inject_pit_timer_intr(kvm);
  576. }
  577. }