i8254.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 <linux/workqueue.h>
  36. #include "irq.h"
  37. #include "i8254.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->pit_timer.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->pit_timer.timer);
  109. elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
  110. elapsed = mod_64(elapsed, ps->pit_timer.period);
  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->pit_timer.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->pit_timer.pending);
  212. else if (value > 0)
  213. /* in this case, we had multiple outstanding pit interrupts
  214. * that we needed to inject. Reinject
  215. */
  216. queue_work(ps->pit->wq, &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.pit_timer.timer;
  227. if (hrtimer_cancel(timer))
  228. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  229. }
  230. static void destroy_pit_timer(struct kvm_pit *pit)
  231. {
  232. hrtimer_cancel(&pit->pit_state.pit_timer.timer);
  233. cancel_work_sync(&pit->expired);
  234. }
  235. static bool kpit_is_periodic(struct kvm_timer *ktimer)
  236. {
  237. struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
  238. pit_timer);
  239. return ps->is_periodic;
  240. }
  241. static struct kvm_timer_ops kpit_ops = {
  242. .is_periodic = kpit_is_periodic,
  243. };
  244. static void pit_do_work(struct work_struct *work)
  245. {
  246. struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
  247. struct kvm *kvm = pit->kvm;
  248. struct kvm_vcpu *vcpu;
  249. int i;
  250. struct kvm_kpit_state *ps = &pit->pit_state;
  251. int inject = 0;
  252. /* Try to inject pending interrupts when
  253. * last one has been acked.
  254. */
  255. spin_lock(&ps->inject_lock);
  256. if (ps->irq_ack) {
  257. ps->irq_ack = 0;
  258. inject = 1;
  259. }
  260. spin_unlock(&ps->inject_lock);
  261. if (inject) {
  262. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
  263. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
  264. /*
  265. * Provides NMI watchdog support via Virtual Wire mode.
  266. * The route is: PIT -> PIC -> LVT0 in NMI mode.
  267. *
  268. * Note: Our Virtual Wire implementation is simplified, only
  269. * propagating PIT interrupts to all VCPUs when they have set
  270. * LVT0 to NMI delivery. Other PIC interrupts are just sent to
  271. * VCPU0, and only if its LVT0 is in EXTINT mode.
  272. */
  273. if (kvm->arch.vapics_in_nmi_mode > 0)
  274. kvm_for_each_vcpu(i, vcpu, kvm)
  275. kvm_apic_nmi_wd_deliver(vcpu);
  276. }
  277. }
  278. static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
  279. {
  280. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  281. struct kvm_pit *pt = ktimer->kvm->arch.vpit;
  282. if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
  283. atomic_inc(&ktimer->pending);
  284. queue_work(pt->wq, &pt->expired);
  285. }
  286. if (ktimer->t_ops->is_periodic(ktimer)) {
  287. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  288. return HRTIMER_RESTART;
  289. } else
  290. return HRTIMER_NORESTART;
  291. }
  292. static void create_pit_timer(struct kvm *kvm, u32 val, int is_period)
  293. {
  294. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  295. struct kvm_timer *pt = &ps->pit_timer;
  296. s64 interval;
  297. if (!irqchip_in_kernel(kvm))
  298. return;
  299. interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
  300. pr_debug("create pit timer, interval is %llu nsec\n", interval);
  301. /* TODO The new value only affected after the retriggered */
  302. hrtimer_cancel(&pt->timer);
  303. cancel_work_sync(&ps->pit->expired);
  304. pt->period = interval;
  305. ps->is_periodic = is_period;
  306. pt->timer.function = pit_timer_fn;
  307. pt->t_ops = &kpit_ops;
  308. pt->kvm = ps->pit->kvm;
  309. atomic_set(&pt->pending, 0);
  310. ps->irq_ack = 1;
  311. hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
  312. HRTIMER_MODE_ABS);
  313. }
  314. static void pit_load_count(struct kvm *kvm, int channel, u32 val)
  315. {
  316. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  317. WARN_ON(!mutex_is_locked(&ps->lock));
  318. pr_debug("load_count val is %d, channel is %d\n", val, channel);
  319. /*
  320. * The largest possible initial count is 0; this is equivalent
  321. * to 216 for binary counting and 104 for BCD counting.
  322. */
  323. if (val == 0)
  324. val = 0x10000;
  325. ps->channels[channel].count = val;
  326. if (channel != 0) {
  327. ps->channels[channel].count_load_time = ktime_get();
  328. return;
  329. }
  330. /* Two types of timer
  331. * mode 1 is one shot, mode 2 is period, otherwise del timer */
  332. switch (ps->channels[0].mode) {
  333. case 0:
  334. case 1:
  335. /* FIXME: enhance mode 4 precision */
  336. case 4:
  337. if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)) {
  338. create_pit_timer(kvm, val, 0);
  339. }
  340. break;
  341. case 2:
  342. case 3:
  343. if (!(ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)){
  344. create_pit_timer(kvm, val, 1);
  345. }
  346. break;
  347. default:
  348. destroy_pit_timer(kvm->arch.vpit);
  349. }
  350. }
  351. void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start)
  352. {
  353. u8 saved_mode;
  354. if (hpet_legacy_start) {
  355. /* save existing mode for later reenablement */
  356. saved_mode = kvm->arch.vpit->pit_state.channels[0].mode;
  357. kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */
  358. pit_load_count(kvm, channel, val);
  359. kvm->arch.vpit->pit_state.channels[0].mode = saved_mode;
  360. } else {
  361. pit_load_count(kvm, channel, val);
  362. }
  363. }
  364. static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
  365. {
  366. return container_of(dev, struct kvm_pit, dev);
  367. }
  368. static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
  369. {
  370. return container_of(dev, struct kvm_pit, speaker_dev);
  371. }
  372. static inline int pit_in_range(gpa_t addr)
  373. {
  374. return ((addr >= KVM_PIT_BASE_ADDRESS) &&
  375. (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
  376. }
  377. static int pit_ioport_write(struct kvm_io_device *this,
  378. gpa_t addr, int len, const void *data)
  379. {
  380. struct kvm_pit *pit = dev_to_pit(this);
  381. struct kvm_kpit_state *pit_state = &pit->pit_state;
  382. struct kvm *kvm = pit->kvm;
  383. int channel, access;
  384. struct kvm_kpit_channel_state *s;
  385. u32 val = *(u32 *) data;
  386. if (!pit_in_range(addr))
  387. return -EOPNOTSUPP;
  388. val &= 0xff;
  389. addr &= KVM_PIT_CHANNEL_MASK;
  390. mutex_lock(&pit_state->lock);
  391. if (val != 0)
  392. pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
  393. (unsigned int)addr, len, val);
  394. if (addr == 3) {
  395. channel = val >> 6;
  396. if (channel == 3) {
  397. /* Read-Back Command. */
  398. for (channel = 0; channel < 3; channel++) {
  399. s = &pit_state->channels[channel];
  400. if (val & (2 << channel)) {
  401. if (!(val & 0x20))
  402. pit_latch_count(kvm, channel);
  403. if (!(val & 0x10))
  404. pit_latch_status(kvm, channel);
  405. }
  406. }
  407. } else {
  408. /* Select Counter <channel>. */
  409. s = &pit_state->channels[channel];
  410. access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
  411. if (access == 0) {
  412. pit_latch_count(kvm, channel);
  413. } else {
  414. s->rw_mode = access;
  415. s->read_state = access;
  416. s->write_state = access;
  417. s->mode = (val >> 1) & 7;
  418. if (s->mode > 5)
  419. s->mode -= 4;
  420. s->bcd = val & 1;
  421. }
  422. }
  423. } else {
  424. /* Write Count. */
  425. s = &pit_state->channels[addr];
  426. switch (s->write_state) {
  427. default:
  428. case RW_STATE_LSB:
  429. pit_load_count(kvm, addr, val);
  430. break;
  431. case RW_STATE_MSB:
  432. pit_load_count(kvm, addr, val << 8);
  433. break;
  434. case RW_STATE_WORD0:
  435. s->write_latch = val;
  436. s->write_state = RW_STATE_WORD1;
  437. break;
  438. case RW_STATE_WORD1:
  439. pit_load_count(kvm, addr, s->write_latch | (val << 8));
  440. s->write_state = RW_STATE_WORD0;
  441. break;
  442. }
  443. }
  444. mutex_unlock(&pit_state->lock);
  445. return 0;
  446. }
  447. static int pit_ioport_read(struct kvm_io_device *this,
  448. gpa_t addr, int len, void *data)
  449. {
  450. struct kvm_pit *pit = dev_to_pit(this);
  451. struct kvm_kpit_state *pit_state = &pit->pit_state;
  452. struct kvm *kvm = pit->kvm;
  453. int ret, count;
  454. struct kvm_kpit_channel_state *s;
  455. if (!pit_in_range(addr))
  456. return -EOPNOTSUPP;
  457. addr &= KVM_PIT_CHANNEL_MASK;
  458. if (addr == 3)
  459. return 0;
  460. s = &pit_state->channels[addr];
  461. mutex_lock(&pit_state->lock);
  462. if (s->status_latched) {
  463. s->status_latched = 0;
  464. ret = s->status;
  465. } else if (s->count_latched) {
  466. switch (s->count_latched) {
  467. default:
  468. case RW_STATE_LSB:
  469. ret = s->latched_count & 0xff;
  470. s->count_latched = 0;
  471. break;
  472. case RW_STATE_MSB:
  473. ret = s->latched_count >> 8;
  474. s->count_latched = 0;
  475. break;
  476. case RW_STATE_WORD0:
  477. ret = s->latched_count & 0xff;
  478. s->count_latched = RW_STATE_MSB;
  479. break;
  480. }
  481. } else {
  482. switch (s->read_state) {
  483. default:
  484. case RW_STATE_LSB:
  485. count = pit_get_count(kvm, addr);
  486. ret = count & 0xff;
  487. break;
  488. case RW_STATE_MSB:
  489. count = pit_get_count(kvm, addr);
  490. ret = (count >> 8) & 0xff;
  491. break;
  492. case RW_STATE_WORD0:
  493. count = pit_get_count(kvm, addr);
  494. ret = count & 0xff;
  495. s->read_state = RW_STATE_WORD1;
  496. break;
  497. case RW_STATE_WORD1:
  498. count = pit_get_count(kvm, addr);
  499. ret = (count >> 8) & 0xff;
  500. s->read_state = RW_STATE_WORD0;
  501. break;
  502. }
  503. }
  504. if (len > sizeof(ret))
  505. len = sizeof(ret);
  506. memcpy(data, (char *)&ret, len);
  507. mutex_unlock(&pit_state->lock);
  508. return 0;
  509. }
  510. static int speaker_ioport_write(struct kvm_io_device *this,
  511. gpa_t addr, int len, const void *data)
  512. {
  513. struct kvm_pit *pit = speaker_to_pit(this);
  514. struct kvm_kpit_state *pit_state = &pit->pit_state;
  515. struct kvm *kvm = pit->kvm;
  516. u32 val = *(u32 *) data;
  517. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  518. return -EOPNOTSUPP;
  519. mutex_lock(&pit_state->lock);
  520. pit_state->speaker_data_on = (val >> 1) & 1;
  521. pit_set_gate(kvm, 2, val & 1);
  522. mutex_unlock(&pit_state->lock);
  523. return 0;
  524. }
  525. static int speaker_ioport_read(struct kvm_io_device *this,
  526. gpa_t addr, int len, void *data)
  527. {
  528. struct kvm_pit *pit = speaker_to_pit(this);
  529. struct kvm_kpit_state *pit_state = &pit->pit_state;
  530. struct kvm *kvm = pit->kvm;
  531. unsigned int refresh_clock;
  532. int ret;
  533. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  534. return -EOPNOTSUPP;
  535. /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
  536. refresh_clock = ((unsigned int)ktime_to_ns(ktime_get()) >> 14) & 1;
  537. mutex_lock(&pit_state->lock);
  538. ret = ((pit_state->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
  539. (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
  540. if (len > sizeof(ret))
  541. len = sizeof(ret);
  542. memcpy(data, (char *)&ret, len);
  543. mutex_unlock(&pit_state->lock);
  544. return 0;
  545. }
  546. void kvm_pit_reset(struct kvm_pit *pit)
  547. {
  548. int i;
  549. struct kvm_kpit_channel_state *c;
  550. mutex_lock(&pit->pit_state.lock);
  551. pit->pit_state.flags = 0;
  552. for (i = 0; i < 3; i++) {
  553. c = &pit->pit_state.channels[i];
  554. c->mode = 0xff;
  555. c->gate = (i != 2);
  556. pit_load_count(pit->kvm, i, 0);
  557. }
  558. mutex_unlock(&pit->pit_state.lock);
  559. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  560. pit->pit_state.irq_ack = 1;
  561. }
  562. static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
  563. {
  564. struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
  565. if (!mask) {
  566. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  567. pit->pit_state.irq_ack = 1;
  568. }
  569. }
  570. static const struct kvm_io_device_ops pit_dev_ops = {
  571. .read = pit_ioport_read,
  572. .write = pit_ioport_write,
  573. };
  574. static const struct kvm_io_device_ops speaker_dev_ops = {
  575. .read = speaker_ioport_read,
  576. .write = speaker_ioport_write,
  577. };
  578. /* Caller must hold slots_lock */
  579. struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
  580. {
  581. struct kvm_pit *pit;
  582. struct kvm_kpit_state *pit_state;
  583. int ret;
  584. pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
  585. if (!pit)
  586. return NULL;
  587. pit->irq_source_id = kvm_request_irq_source_id(kvm);
  588. if (pit->irq_source_id < 0) {
  589. kfree(pit);
  590. return NULL;
  591. }
  592. mutex_init(&pit->pit_state.lock);
  593. mutex_lock(&pit->pit_state.lock);
  594. spin_lock_init(&pit->pit_state.inject_lock);
  595. pit->wq = create_singlethread_workqueue("kvm-pit-wq");
  596. if (!pit->wq) {
  597. mutex_unlock(&pit->pit_state.lock);
  598. kvm_free_irq_source_id(kvm, pit->irq_source_id);
  599. kfree(pit);
  600. return NULL;
  601. }
  602. INIT_WORK(&pit->expired, pit_do_work);
  603. kvm->arch.vpit = pit;
  604. pit->kvm = kvm;
  605. pit_state = &pit->pit_state;
  606. pit_state->pit = pit;
  607. hrtimer_init(&pit_state->pit_timer.timer,
  608. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  609. pit_state->irq_ack_notifier.gsi = 0;
  610. pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
  611. kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
  612. pit_state->pit_timer.reinject = true;
  613. mutex_unlock(&pit->pit_state.lock);
  614. kvm_pit_reset(pit);
  615. pit->mask_notifier.func = pit_mask_notifer;
  616. kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  617. kvm_iodevice_init(&pit->dev, &pit_dev_ops);
  618. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
  619. KVM_PIT_MEM_LENGTH, &pit->dev);
  620. if (ret < 0)
  621. goto fail;
  622. if (flags & KVM_PIT_SPEAKER_DUMMY) {
  623. kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
  624. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
  625. KVM_SPEAKER_BASE_ADDRESS, 4,
  626. &pit->speaker_dev);
  627. if (ret < 0)
  628. goto fail_unregister;
  629. }
  630. return pit;
  631. fail_unregister:
  632. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
  633. fail:
  634. kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  635. kvm_unregister_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
  636. kvm_free_irq_source_id(kvm, pit->irq_source_id);
  637. destroy_workqueue(pit->wq);
  638. kfree(pit);
  639. return NULL;
  640. }
  641. void kvm_free_pit(struct kvm *kvm)
  642. {
  643. struct hrtimer *timer;
  644. if (kvm->arch.vpit) {
  645. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &kvm->arch.vpit->dev);
  646. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  647. &kvm->arch.vpit->speaker_dev);
  648. kvm_unregister_irq_mask_notifier(kvm, 0,
  649. &kvm->arch.vpit->mask_notifier);
  650. kvm_unregister_irq_ack_notifier(kvm,
  651. &kvm->arch.vpit->pit_state.irq_ack_notifier);
  652. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  653. timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
  654. hrtimer_cancel(timer);
  655. cancel_work_sync(&kvm->arch.vpit->expired);
  656. kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
  657. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  658. destroy_workqueue(kvm->arch.vpit->wq);
  659. kfree(kvm->arch.vpit);
  660. }
  661. }