i8254.c 18 KB

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