i8254.c 19 KB

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