i8254.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 <[email protected]>
  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. /* Somewhat arbitrarily make vcpu0 the owner of the PIT. */
  193. if (vcpu->vcpu_id || !pit)
  194. return;
  195. timer = &pit->pit_state.timer;
  196. mutex_lock(&pit->pit_state.lock);
  197. if (hrtimer_cancel(timer))
  198. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  199. mutex_unlock(&pit->pit_state.lock);
  200. }
  201. static void destroy_pit_timer(struct kvm_pit *pit)
  202. {
  203. hrtimer_cancel(&pit->pit_state.timer);
  204. kthread_flush_work(&pit->expired);
  205. }
  206. static void pit_do_work(struct kthread_work *work)
  207. {
  208. struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
  209. struct kvm *kvm = pit->kvm;
  210. struct kvm_vcpu *vcpu;
  211. unsigned long i;
  212. struct kvm_kpit_state *ps = &pit->pit_state;
  213. if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0))
  214. return;
  215. kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false);
  216. kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false);
  217. /*
  218. * Provides NMI watchdog support via Virtual Wire mode.
  219. * The route is: PIT -> LVT0 in NMI mode.
  220. *
  221. * Note: Our Virtual Wire implementation does not follow
  222. * the MP specification. We propagate a PIT interrupt to all
  223. * VCPUs and only when LVT0 is in NMI mode. The interrupt can
  224. * also be simultaneously delivered through PIC and IOAPIC.
  225. */
  226. if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0)
  227. kvm_for_each_vcpu(i, vcpu, kvm)
  228. kvm_apic_nmi_wd_deliver(vcpu);
  229. }
  230. static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
  231. {
  232. struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer);
  233. struct kvm_pit *pt = pit_state_to_pit(ps);
  234. if (atomic_read(&ps->reinject))
  235. atomic_inc(&ps->pending);
  236. kthread_queue_work(pt->worker, &pt->expired);
  237. if (ps->is_periodic) {
  238. hrtimer_add_expires_ns(&ps->timer, ps->period);
  239. return HRTIMER_RESTART;
  240. } else
  241. return HRTIMER_NORESTART;
  242. }
  243. static inline void kvm_pit_reset_reinject(struct kvm_pit *pit)
  244. {
  245. atomic_set(&pit->pit_state.pending, 0);
  246. atomic_set(&pit->pit_state.irq_ack, 1);
  247. }
  248. void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject)
  249. {
  250. struct kvm_kpit_state *ps = &pit->pit_state;
  251. struct kvm *kvm = pit->kvm;
  252. if (atomic_read(&ps->reinject) == reinject)
  253. return;
  254. /*
  255. * AMD SVM AVIC accelerates EOI write and does not trap.
  256. * This cause in-kernel PIT re-inject mode to fail
  257. * since it checks ps->irq_ack before kvm_set_irq()
  258. * and relies on the ack notifier to timely queue
  259. * the pt->worker work iterm and reinject the missed tick.
  260. * So, deactivate APICv when PIT is in reinject mode.
  261. */
  262. if (reinject) {
  263. kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PIT_REINJ);
  264. /* The initial state is preserved while ps->reinject == 0. */
  265. kvm_pit_reset_reinject(pit);
  266. kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
  267. kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  268. } else {
  269. kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PIT_REINJ);
  270. kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
  271. kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  272. }
  273. atomic_set(&ps->reinject, reinject);
  274. }
  275. static void create_pit_timer(struct kvm_pit *pit, u32 val, int is_period)
  276. {
  277. struct kvm_kpit_state *ps = &pit->pit_state;
  278. struct kvm *kvm = pit->kvm;
  279. s64 interval;
  280. if (!ioapic_in_kernel(kvm) ||
  281. ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)
  282. return;
  283. interval = mul_u64_u32_div(val, NSEC_PER_SEC, KVM_PIT_FREQ);
  284. pr_debug("create pit timer, interval is %llu nsec\n", interval);
  285. /* TODO The new value only affected after the retriggered */
  286. hrtimer_cancel(&ps->timer);
  287. kthread_flush_work(&pit->expired);
  288. ps->period = interval;
  289. ps->is_periodic = is_period;
  290. kvm_pit_reset_reinject(pit);
  291. /*
  292. * Do not allow the guest to program periodic timers with small
  293. * interval, since the hrtimers are not throttled by the host
  294. * scheduler.
  295. */
  296. if (ps->is_periodic) {
  297. s64 min_period = min_timer_period_us * 1000LL;
  298. if (ps->period < min_period) {
  299. pr_info_ratelimited(
  300. "kvm: requested %lld ns "
  301. "i8254 timer period limited to %lld ns\n",
  302. ps->period, min_period);
  303. ps->period = min_period;
  304. }
  305. }
  306. hrtimer_start(&ps->timer, ktime_add_ns(ktime_get(), interval),
  307. HRTIMER_MODE_ABS);
  308. }
  309. static void pit_load_count(struct kvm_pit *pit, int channel, u32 val)
  310. {
  311. struct kvm_kpit_state *ps = &pit->pit_state;
  312. pr_debug("load_count val is %u, channel is %d\n", val, channel);
  313. /*
  314. * The largest possible initial count is 0; this is equivalent
  315. * to 216 for binary counting and 104 for BCD counting.
  316. */
  317. if (val == 0)
  318. val = 0x10000;
  319. ps->channels[channel].count = val;
  320. if (channel != 0) {
  321. ps->channels[channel].count_load_time = ktime_get();
  322. return;
  323. }
  324. /* Two types of timer
  325. * mode 1 is one shot, mode 2 is period, otherwise del timer */
  326. switch (ps->channels[0].mode) {
  327. case 0:
  328. case 1:
  329. /* FIXME: enhance mode 4 precision */
  330. case 4:
  331. create_pit_timer(pit, val, 0);
  332. break;
  333. case 2:
  334. case 3:
  335. create_pit_timer(pit, val, 1);
  336. break;
  337. default:
  338. destroy_pit_timer(pit);
  339. }
  340. }
  341. void kvm_pit_load_count(struct kvm_pit *pit, int channel, u32 val,
  342. int hpet_legacy_start)
  343. {
  344. u8 saved_mode;
  345. WARN_ON_ONCE(!mutex_is_locked(&pit->pit_state.lock));
  346. if (hpet_legacy_start) {
  347. /* save existing mode for later reenablement */
  348. WARN_ON(channel != 0);
  349. saved_mode = pit->pit_state.channels[0].mode;
  350. pit->pit_state.channels[0].mode = 0xff; /* disable timer */
  351. pit_load_count(pit, channel, val);
  352. pit->pit_state.channels[0].mode = saved_mode;
  353. } else {
  354. pit_load_count(pit, channel, val);
  355. }
  356. }
  357. static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
  358. {
  359. return container_of(dev, struct kvm_pit, dev);
  360. }
  361. static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
  362. {
  363. return container_of(dev, struct kvm_pit, speaker_dev);
  364. }
  365. static inline int pit_in_range(gpa_t addr)
  366. {
  367. return ((addr >= KVM_PIT_BASE_ADDRESS) &&
  368. (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
  369. }
  370. static int pit_ioport_write(struct kvm_vcpu *vcpu,
  371. struct kvm_io_device *this,
  372. gpa_t addr, int len, const void *data)
  373. {
  374. struct kvm_pit *pit = dev_to_pit(this);
  375. struct kvm_kpit_state *pit_state = &pit->pit_state;
  376. int channel, access;
  377. struct kvm_kpit_channel_state *s;
  378. u32 val = *(u32 *) data;
  379. if (!pit_in_range(addr))
  380. return -EOPNOTSUPP;
  381. val &= 0xff;
  382. addr &= KVM_PIT_CHANNEL_MASK;
  383. mutex_lock(&pit_state->lock);
  384. if (val != 0)
  385. pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
  386. (unsigned int)addr, len, val);
  387. if (addr == 3) {
  388. channel = val >> 6;
  389. if (channel == 3) {
  390. /* Read-Back Command. */
  391. for (channel = 0; channel < 3; channel++) {
  392. if (val & (2 << channel)) {
  393. if (!(val & 0x20))
  394. pit_latch_count(pit, channel);
  395. if (!(val & 0x10))
  396. pit_latch_status(pit, channel);
  397. }
  398. }
  399. } else {
  400. /* Select Counter <channel>. */
  401. s = &pit_state->channels[channel];
  402. access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
  403. if (access == 0) {
  404. pit_latch_count(pit, channel);
  405. } else {
  406. s->rw_mode = access;
  407. s->read_state = access;
  408. s->write_state = access;
  409. s->mode = (val >> 1) & 7;
  410. if (s->mode > 5)
  411. s->mode -= 4;
  412. s->bcd = val & 1;
  413. }
  414. }
  415. } else {
  416. /* Write Count. */
  417. s = &pit_state->channels[addr];
  418. switch (s->write_state) {
  419. default:
  420. case RW_STATE_LSB:
  421. pit_load_count(pit, addr, val);
  422. break;
  423. case RW_STATE_MSB:
  424. pit_load_count(pit, addr, val << 8);
  425. break;
  426. case RW_STATE_WORD0:
  427. s->write_latch = val;
  428. s->write_state = RW_STATE_WORD1;
  429. break;
  430. case RW_STATE_WORD1:
  431. pit_load_count(pit, addr, s->write_latch | (val << 8));
  432. s->write_state = RW_STATE_WORD0;
  433. break;
  434. }
  435. }
  436. mutex_unlock(&pit_state->lock);
  437. return 0;
  438. }
  439. static int pit_ioport_read(struct kvm_vcpu *vcpu,
  440. struct kvm_io_device *this,
  441. gpa_t addr, int len, void *data)
  442. {
  443. struct kvm_pit *pit = dev_to_pit(this);
  444. struct kvm_kpit_state *pit_state = &pit->pit_state;
  445. int ret, count;
  446. struct kvm_kpit_channel_state *s;
  447. if (!pit_in_range(addr))
  448. return -EOPNOTSUPP;
  449. addr &= KVM_PIT_CHANNEL_MASK;
  450. if (addr == 3)
  451. return 0;
  452. s = &pit_state->channels[addr];
  453. mutex_lock(&pit_state->lock);
  454. if (s->status_latched) {
  455. s->status_latched = 0;
  456. ret = s->status;
  457. } else if (s->count_latched) {
  458. switch (s->count_latched) {
  459. default:
  460. case RW_STATE_LSB:
  461. ret = s->latched_count & 0xff;
  462. s->count_latched = 0;
  463. break;
  464. case RW_STATE_MSB:
  465. ret = s->latched_count >> 8;
  466. s->count_latched = 0;
  467. break;
  468. case RW_STATE_WORD0:
  469. ret = s->latched_count & 0xff;
  470. s->count_latched = RW_STATE_MSB;
  471. break;
  472. }
  473. } else {
  474. switch (s->read_state) {
  475. default:
  476. case RW_STATE_LSB:
  477. count = pit_get_count(pit, addr);
  478. ret = count & 0xff;
  479. break;
  480. case RW_STATE_MSB:
  481. count = pit_get_count(pit, addr);
  482. ret = (count >> 8) & 0xff;
  483. break;
  484. case RW_STATE_WORD0:
  485. count = pit_get_count(pit, addr);
  486. ret = count & 0xff;
  487. s->read_state = RW_STATE_WORD1;
  488. break;
  489. case RW_STATE_WORD1:
  490. count = pit_get_count(pit, addr);
  491. ret = (count >> 8) & 0xff;
  492. s->read_state = RW_STATE_WORD0;
  493. break;
  494. }
  495. }
  496. if (len > sizeof(ret))
  497. len = sizeof(ret);
  498. memcpy(data, (char *)&ret, len);
  499. mutex_unlock(&pit_state->lock);
  500. return 0;
  501. }
  502. static int speaker_ioport_write(struct kvm_vcpu *vcpu,
  503. struct kvm_io_device *this,
  504. gpa_t addr, int len, const void *data)
  505. {
  506. struct kvm_pit *pit = speaker_to_pit(this);
  507. struct kvm_kpit_state *pit_state = &pit->pit_state;
  508. u32 val = *(u32 *) data;
  509. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  510. return -EOPNOTSUPP;
  511. mutex_lock(&pit_state->lock);
  512. if (val & (1 << 1))
  513. pit_state->flags |= KVM_PIT_FLAGS_SPEAKER_DATA_ON;
  514. else
  515. pit_state->flags &= ~KVM_PIT_FLAGS_SPEAKER_DATA_ON;
  516. pit_set_gate(pit, 2, val & 1);
  517. mutex_unlock(&pit_state->lock);
  518. return 0;
  519. }
  520. static int speaker_ioport_read(struct kvm_vcpu *vcpu,
  521. struct kvm_io_device *this,
  522. gpa_t addr, int len, void *data)
  523. {
  524. struct kvm_pit *pit = speaker_to_pit(this);
  525. struct kvm_kpit_state *pit_state = &pit->pit_state;
  526. unsigned int refresh_clock;
  527. int ret;
  528. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  529. return -EOPNOTSUPP;
  530. /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
  531. refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
  532. mutex_lock(&pit_state->lock);
  533. ret = (!!(pit_state->flags & KVM_PIT_FLAGS_SPEAKER_DATA_ON) << 1) |
  534. pit_get_gate(pit, 2) | (pit_get_out(pit, 2) << 5) |
  535. (refresh_clock << 4);
  536. if (len > sizeof(ret))
  537. len = sizeof(ret);
  538. memcpy(data, (char *)&ret, len);
  539. mutex_unlock(&pit_state->lock);
  540. return 0;
  541. }
  542. static void kvm_pit_reset(struct kvm_pit *pit)
  543. {
  544. int i;
  545. struct kvm_kpit_channel_state *c;
  546. pit->pit_state.flags = 0;
  547. for (i = 0; i < 3; i++) {
  548. c = &pit->pit_state.channels[i];
  549. c->mode = 0xff;
  550. c->gate = (i != 2);
  551. pit_load_count(pit, i, 0);
  552. }
  553. kvm_pit_reset_reinject(pit);
  554. }
  555. static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
  556. {
  557. struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
  558. if (!mask)
  559. kvm_pit_reset_reinject(pit);
  560. }
  561. static const struct kvm_io_device_ops pit_dev_ops = {
  562. .read = pit_ioport_read,
  563. .write = pit_ioport_write,
  564. };
  565. static const struct kvm_io_device_ops speaker_dev_ops = {
  566. .read = speaker_ioport_read,
  567. .write = speaker_ioport_write,
  568. };
  569. struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
  570. {
  571. struct kvm_pit *pit;
  572. struct kvm_kpit_state *pit_state;
  573. struct pid *pid;
  574. pid_t pid_nr;
  575. int ret;
  576. pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL_ACCOUNT);
  577. if (!pit)
  578. return NULL;
  579. pit->irq_source_id = kvm_request_irq_source_id(kvm);
  580. if (pit->irq_source_id < 0)
  581. goto fail_request;
  582. mutex_init(&pit->pit_state.lock);
  583. pid = get_pid(task_tgid(current));
  584. pid_nr = pid_vnr(pid);
  585. put_pid(pid);
  586. pit->worker = kthread_create_worker(0, "kvm-pit/%d", pid_nr);
  587. if (IS_ERR(pit->worker))
  588. goto fail_kthread;
  589. kthread_init_work(&pit->expired, pit_do_work);
  590. pit->kvm = kvm;
  591. pit_state = &pit->pit_state;
  592. hrtimer_init(&pit_state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  593. pit_state->timer.function = pit_timer_fn;
  594. pit_state->irq_ack_notifier.gsi = 0;
  595. pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
  596. pit->mask_notifier.func = pit_mask_notifer;
  597. kvm_pit_reset(pit);
  598. kvm_pit_set_reinject(pit, true);
  599. mutex_lock(&kvm->slots_lock);
  600. kvm_iodevice_init(&pit->dev, &pit_dev_ops);
  601. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
  602. KVM_PIT_MEM_LENGTH, &pit->dev);
  603. if (ret < 0)
  604. goto fail_register_pit;
  605. if (flags & KVM_PIT_SPEAKER_DUMMY) {
  606. kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
  607. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
  608. KVM_SPEAKER_BASE_ADDRESS, 4,
  609. &pit->speaker_dev);
  610. if (ret < 0)
  611. goto fail_register_speaker;
  612. }
  613. mutex_unlock(&kvm->slots_lock);
  614. return pit;
  615. fail_register_speaker:
  616. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
  617. fail_register_pit:
  618. mutex_unlock(&kvm->slots_lock);
  619. kvm_pit_set_reinject(pit, false);
  620. kthread_destroy_worker(pit->worker);
  621. fail_kthread:
  622. kvm_free_irq_source_id(kvm, pit->irq_source_id);
  623. fail_request:
  624. kfree(pit);
  625. return NULL;
  626. }
  627. void kvm_free_pit(struct kvm *kvm)
  628. {
  629. struct kvm_pit *pit = kvm->arch.vpit;
  630. if (pit) {
  631. mutex_lock(&kvm->slots_lock);
  632. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
  633. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->speaker_dev);
  634. mutex_unlock(&kvm->slots_lock);
  635. kvm_pit_set_reinject(pit, false);
  636. hrtimer_cancel(&pit->pit_state.timer);
  637. kthread_destroy_worker(pit->worker);
  638. kvm_free_irq_source_id(kvm, pit->irq_source_id);
  639. kfree(pit);
  640. }
  641. }