i8254.c 19 KB

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