i8254.c 18 KB

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