i8254.c 18 KB

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