i8254.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #ifndef CONFIG_X86_64
  38. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  39. #else
  40. #define mod_64(x, y) ((x) % (y))
  41. #endif
  42. #define RW_STATE_LSB 1
  43. #define RW_STATE_MSB 2
  44. #define RW_STATE_WORD0 3
  45. #define RW_STATE_WORD1 4
  46. /* Compute with 96 bit intermediate result: (a*b)/c */
  47. static u64 muldiv64(u64 a, u32 b, u32 c)
  48. {
  49. union {
  50. u64 ll;
  51. struct {
  52. u32 low, high;
  53. } l;
  54. } u, res;
  55. u64 rl, rh;
  56. u.ll = a;
  57. rl = (u64)u.l.low * (u64)b;
  58. rh = (u64)u.l.high * (u64)b;
  59. rh += (rl >> 32);
  60. res.l.high = div64_u64(rh, c);
  61. res.l.low = div64_u64(((mod_64(rh, c) << 32) + (rl & 0xffffffff)), c);
  62. return res.ll;
  63. }
  64. static void pit_set_gate(struct kvm *kvm, int channel, u32 val)
  65. {
  66. struct kvm_kpit_channel_state *c =
  67. &kvm->arch.vpit->pit_state.channels[channel];
  68. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  69. switch (c->mode) {
  70. default:
  71. case 0:
  72. case 4:
  73. /* XXX: just disable/enable counting */
  74. break;
  75. case 1:
  76. case 2:
  77. case 3:
  78. case 5:
  79. /* Restart counting on rising edge. */
  80. if (c->gate < val)
  81. c->count_load_time = ktime_get();
  82. break;
  83. }
  84. c->gate = val;
  85. }
  86. static int pit_get_gate(struct kvm *kvm, int channel)
  87. {
  88. WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock));
  89. return kvm->arch.vpit->pit_state.channels[channel].gate;
  90. }
  91. static s64 __kpit_elapsed(struct kvm *kvm)
  92. {
  93. s64 elapsed;
  94. ktime_t remaining;
  95. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  96. if (!ps->pit_timer.period)
  97. return 0;
  98. /*
  99. * The Counter does not stop when it reaches zero. In
  100. * Modes 0, 1, 4, and 5 the Counter ``wraps around'' to
  101. * the highest count, either FFFF hex for binary counting
  102. * or 9999 for BCD counting, and continues counting.
  103. * Modes 2 and 3 are periodic; the Counter reloads
  104. * itself with the initial count and continues counting
  105. * from there.
  106. */
  107. remaining = hrtimer_get_remaining(&ps->pit_timer.timer);
  108. elapsed = ps->pit_timer.period - ktime_to_ns(remaining);
  109. elapsed = mod_64(elapsed, ps->pit_timer.period);
  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->pit_timer.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->pit_timer.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.pit_timer.timer;
  226. if (hrtimer_cancel(timer))
  227. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  228. }
  229. static void destroy_pit_timer(struct kvm_pit *pit)
  230. {
  231. hrtimer_cancel(&pit->pit_state.pit_timer.timer);
  232. flush_kthread_work(&pit->expired);
  233. }
  234. static bool kpit_is_periodic(struct kvm_timer *ktimer)
  235. {
  236. struct kvm_kpit_state *ps = container_of(ktimer, struct kvm_kpit_state,
  237. pit_timer);
  238. return ps->is_periodic;
  239. }
  240. static struct kvm_timer_ops kpit_ops = {
  241. .is_periodic = kpit_is_periodic,
  242. };
  243. static void pit_do_work(struct kthread_work *work)
  244. {
  245. struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);
  246. struct kvm *kvm = pit->kvm;
  247. struct kvm_vcpu *vcpu;
  248. int i;
  249. struct kvm_kpit_state *ps = &pit->pit_state;
  250. int inject = 0;
  251. /* Try to inject pending interrupts when
  252. * last one has been acked.
  253. */
  254. spin_lock(&ps->inject_lock);
  255. if (ps->irq_ack) {
  256. ps->irq_ack = 0;
  257. inject = 1;
  258. }
  259. spin_unlock(&ps->inject_lock);
  260. if (inject) {
  261. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
  262. kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
  263. /*
  264. * Provides NMI watchdog support via Virtual Wire mode.
  265. * The route is: PIT -> PIC -> LVT0 in NMI mode.
  266. *
  267. * Note: Our Virtual Wire implementation is simplified, only
  268. * propagating PIT interrupts to all VCPUs when they have set
  269. * LVT0 to NMI delivery. Other PIC interrupts are just sent to
  270. * VCPU0, and only if its LVT0 is in EXTINT mode.
  271. */
  272. if (kvm->arch.vapics_in_nmi_mode > 0)
  273. kvm_for_each_vcpu(i, vcpu, kvm)
  274. kvm_apic_nmi_wd_deliver(vcpu);
  275. }
  276. }
  277. static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
  278. {
  279. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  280. struct kvm_pit *pt = ktimer->kvm->arch.vpit;
  281. if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
  282. atomic_inc(&ktimer->pending);
  283. queue_kthread_work(&pt->worker, &pt->expired);
  284. }
  285. if (ktimer->t_ops->is_periodic(ktimer)) {
  286. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  287. return HRTIMER_RESTART;
  288. } else
  289. return HRTIMER_NORESTART;
  290. }
  291. static void create_pit_timer(struct kvm *kvm, u32 val, int is_period)
  292. {
  293. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  294. struct kvm_timer *pt = &ps->pit_timer;
  295. s64 interval;
  296. if (!irqchip_in_kernel(kvm) || ps->flags & KVM_PIT_FLAGS_HPET_LEGACY)
  297. return;
  298. interval = muldiv64(val, NSEC_PER_SEC, KVM_PIT_FREQ);
  299. pr_debug("create pit timer, interval is %llu nsec\n", interval);
  300. /* TODO The new value only affected after the retriggered */
  301. hrtimer_cancel(&pt->timer);
  302. flush_kthread_work(&ps->pit->expired);
  303. pt->period = interval;
  304. ps->is_periodic = is_period;
  305. pt->timer.function = pit_timer_fn;
  306. pt->t_ops = &kpit_ops;
  307. pt->kvm = ps->pit->kvm;
  308. atomic_set(&pt->pending, 0);
  309. ps->irq_ack = 1;
  310. hrtimer_start(&pt->timer, ktime_add_ns(ktime_get(), interval),
  311. HRTIMER_MODE_ABS);
  312. }
  313. static void pit_load_count(struct kvm *kvm, int channel, u32 val)
  314. {
  315. struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state;
  316. WARN_ON(!mutex_is_locked(&ps->lock));
  317. pr_debug("load_count val is %d, channel is %d\n", val, channel);
  318. /*
  319. * The largest possible initial count is 0; this is equivalent
  320. * to 216 for binary counting and 104 for BCD counting.
  321. */
  322. if (val == 0)
  323. val = 0x10000;
  324. ps->channels[channel].count = val;
  325. if (channel != 0) {
  326. ps->channels[channel].count_load_time = ktime_get();
  327. return;
  328. }
  329. /* Two types of timer
  330. * mode 1 is one shot, mode 2 is period, otherwise del timer */
  331. switch (ps->channels[0].mode) {
  332. case 0:
  333. case 1:
  334. /* FIXME: enhance mode 4 precision */
  335. case 4:
  336. create_pit_timer(kvm, val, 0);
  337. break;
  338. case 2:
  339. case 3:
  340. create_pit_timer(kvm, val, 1);
  341. break;
  342. default:
  343. destroy_pit_timer(kvm->arch.vpit);
  344. }
  345. }
  346. void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int hpet_legacy_start)
  347. {
  348. u8 saved_mode;
  349. if (hpet_legacy_start) {
  350. /* save existing mode for later reenablement */
  351. saved_mode = kvm->arch.vpit->pit_state.channels[0].mode;
  352. kvm->arch.vpit->pit_state.channels[0].mode = 0xff; /* disable timer */
  353. pit_load_count(kvm, channel, val);
  354. kvm->arch.vpit->pit_state.channels[0].mode = saved_mode;
  355. } else {
  356. pit_load_count(kvm, channel, val);
  357. }
  358. }
  359. static inline struct kvm_pit *dev_to_pit(struct kvm_io_device *dev)
  360. {
  361. return container_of(dev, struct kvm_pit, dev);
  362. }
  363. static inline struct kvm_pit *speaker_to_pit(struct kvm_io_device *dev)
  364. {
  365. return container_of(dev, struct kvm_pit, speaker_dev);
  366. }
  367. static inline int pit_in_range(gpa_t addr)
  368. {
  369. return ((addr >= KVM_PIT_BASE_ADDRESS) &&
  370. (addr < KVM_PIT_BASE_ADDRESS + KVM_PIT_MEM_LENGTH));
  371. }
  372. static int pit_ioport_write(struct kvm_io_device *this,
  373. gpa_t addr, int len, const void *data)
  374. {
  375. struct kvm_pit *pit = dev_to_pit(this);
  376. struct kvm_kpit_state *pit_state = &pit->pit_state;
  377. struct kvm *kvm = pit->kvm;
  378. int channel, access;
  379. struct kvm_kpit_channel_state *s;
  380. u32 val = *(u32 *) data;
  381. if (!pit_in_range(addr))
  382. return -EOPNOTSUPP;
  383. val &= 0xff;
  384. addr &= KVM_PIT_CHANNEL_MASK;
  385. mutex_lock(&pit_state->lock);
  386. if (val != 0)
  387. pr_debug("write addr is 0x%x, len is %d, val is 0x%x\n",
  388. (unsigned int)addr, len, val);
  389. if (addr == 3) {
  390. channel = val >> 6;
  391. if (channel == 3) {
  392. /* Read-Back Command. */
  393. for (channel = 0; channel < 3; channel++) {
  394. s = &pit_state->channels[channel];
  395. if (val & (2 << channel)) {
  396. if (!(val & 0x20))
  397. pit_latch_count(kvm, channel);
  398. if (!(val & 0x10))
  399. pit_latch_status(kvm, channel);
  400. }
  401. }
  402. } else {
  403. /* Select Counter <channel>. */
  404. s = &pit_state->channels[channel];
  405. access = (val >> 4) & KVM_PIT_CHANNEL_MASK;
  406. if (access == 0) {
  407. pit_latch_count(kvm, channel);
  408. } else {
  409. s->rw_mode = access;
  410. s->read_state = access;
  411. s->write_state = access;
  412. s->mode = (val >> 1) & 7;
  413. if (s->mode > 5)
  414. s->mode -= 4;
  415. s->bcd = val & 1;
  416. }
  417. }
  418. } else {
  419. /* Write Count. */
  420. s = &pit_state->channels[addr];
  421. switch (s->write_state) {
  422. default:
  423. case RW_STATE_LSB:
  424. pit_load_count(kvm, addr, val);
  425. break;
  426. case RW_STATE_MSB:
  427. pit_load_count(kvm, addr, val << 8);
  428. break;
  429. case RW_STATE_WORD0:
  430. s->write_latch = val;
  431. s->write_state = RW_STATE_WORD1;
  432. break;
  433. case RW_STATE_WORD1:
  434. pit_load_count(kvm, addr, s->write_latch | (val << 8));
  435. s->write_state = RW_STATE_WORD0;
  436. break;
  437. }
  438. }
  439. mutex_unlock(&pit_state->lock);
  440. return 0;
  441. }
  442. static int pit_ioport_read(struct kvm_io_device *this,
  443. gpa_t addr, int len, void *data)
  444. {
  445. struct kvm_pit *pit = dev_to_pit(this);
  446. struct kvm_kpit_state *pit_state = &pit->pit_state;
  447. struct kvm *kvm = pit->kvm;
  448. int ret, count;
  449. struct kvm_kpit_channel_state *s;
  450. if (!pit_in_range(addr))
  451. return -EOPNOTSUPP;
  452. addr &= KVM_PIT_CHANNEL_MASK;
  453. if (addr == 3)
  454. return 0;
  455. s = &pit_state->channels[addr];
  456. mutex_lock(&pit_state->lock);
  457. if (s->status_latched) {
  458. s->status_latched = 0;
  459. ret = s->status;
  460. } else if (s->count_latched) {
  461. switch (s->count_latched) {
  462. default:
  463. case RW_STATE_LSB:
  464. ret = s->latched_count & 0xff;
  465. s->count_latched = 0;
  466. break;
  467. case RW_STATE_MSB:
  468. ret = s->latched_count >> 8;
  469. s->count_latched = 0;
  470. break;
  471. case RW_STATE_WORD0:
  472. ret = s->latched_count & 0xff;
  473. s->count_latched = RW_STATE_MSB;
  474. break;
  475. }
  476. } else {
  477. switch (s->read_state) {
  478. default:
  479. case RW_STATE_LSB:
  480. count = pit_get_count(kvm, addr);
  481. ret = count & 0xff;
  482. break;
  483. case RW_STATE_MSB:
  484. count = pit_get_count(kvm, addr);
  485. ret = (count >> 8) & 0xff;
  486. break;
  487. case RW_STATE_WORD0:
  488. count = pit_get_count(kvm, addr);
  489. ret = count & 0xff;
  490. s->read_state = RW_STATE_WORD1;
  491. break;
  492. case RW_STATE_WORD1:
  493. count = pit_get_count(kvm, addr);
  494. ret = (count >> 8) & 0xff;
  495. s->read_state = RW_STATE_WORD0;
  496. break;
  497. }
  498. }
  499. if (len > sizeof(ret))
  500. len = sizeof(ret);
  501. memcpy(data, (char *)&ret, len);
  502. mutex_unlock(&pit_state->lock);
  503. return 0;
  504. }
  505. static int speaker_ioport_write(struct kvm_io_device *this,
  506. gpa_t addr, int len, const void *data)
  507. {
  508. struct kvm_pit *pit = speaker_to_pit(this);
  509. struct kvm_kpit_state *pit_state = &pit->pit_state;
  510. struct kvm *kvm = pit->kvm;
  511. u32 val = *(u32 *) data;
  512. if (addr != KVM_SPEAKER_BASE_ADDRESS)
  513. return -EOPNOTSUPP;
  514. mutex_lock(&pit_state->lock);
  515. pit_state->speaker_data_on = (val >> 1) & 1;
  516. pit_set_gate(kvm, 2, val & 1);
  517. mutex_unlock(&pit_state->lock);
  518. return 0;
  519. }
  520. static int speaker_ioport_read(struct kvm_io_device *this,
  521. gpa_t addr, int len, void *data)
  522. {
  523. struct kvm_pit *pit = speaker_to_pit(this);
  524. struct kvm_kpit_state *pit_state = &pit->pit_state;
  525. struct kvm *kvm = pit->kvm;
  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->speaker_data_on << 1) | pit_get_gate(kvm, 2) |
  534. (pit_get_out(kvm, 2) << 5) | (refresh_clock << 4));
  535. if (len > sizeof(ret))
  536. len = sizeof(ret);
  537. memcpy(data, (char *)&ret, len);
  538. mutex_unlock(&pit_state->lock);
  539. return 0;
  540. }
  541. void kvm_pit_reset(struct kvm_pit *pit)
  542. {
  543. int i;
  544. struct kvm_kpit_channel_state *c;
  545. mutex_lock(&pit->pit_state.lock);
  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->kvm, i, 0);
  552. }
  553. mutex_unlock(&pit->pit_state.lock);
  554. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  555. pit->pit_state.irq_ack = 1;
  556. }
  557. static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask)
  558. {
  559. struct kvm_pit *pit = container_of(kimn, struct kvm_pit, mask_notifier);
  560. if (!mask) {
  561. atomic_set(&pit->pit_state.pit_timer.pending, 0);
  562. pit->pit_state.irq_ack = 1;
  563. }
  564. }
  565. static const struct kvm_io_device_ops pit_dev_ops = {
  566. .read = pit_ioport_read,
  567. .write = pit_ioport_write,
  568. };
  569. static const struct kvm_io_device_ops speaker_dev_ops = {
  570. .read = speaker_ioport_read,
  571. .write = speaker_ioport_write,
  572. };
  573. /* Caller must hold slots_lock */
  574. struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
  575. {
  576. struct kvm_pit *pit;
  577. struct kvm_kpit_state *pit_state;
  578. struct pid *pid;
  579. pid_t pid_nr;
  580. int ret;
  581. pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
  582. if (!pit)
  583. return NULL;
  584. pit->irq_source_id = kvm_request_irq_source_id(kvm);
  585. if (pit->irq_source_id < 0) {
  586. kfree(pit);
  587. return NULL;
  588. }
  589. mutex_init(&pit->pit_state.lock);
  590. mutex_lock(&pit->pit_state.lock);
  591. spin_lock_init(&pit->pit_state.inject_lock);
  592. pid = get_pid(task_tgid(current));
  593. pid_nr = pid_vnr(pid);
  594. put_pid(pid);
  595. init_kthread_worker(&pit->worker);
  596. pit->worker_task = kthread_run(kthread_worker_fn, &pit->worker,
  597. "kvm-pit/%d", pid_nr);
  598. if (IS_ERR(pit->worker_task)) {
  599. mutex_unlock(&pit->pit_state.lock);
  600. kvm_free_irq_source_id(kvm, pit->irq_source_id);
  601. kfree(pit);
  602. return NULL;
  603. }
  604. init_kthread_work(&pit->expired, pit_do_work);
  605. kvm->arch.vpit = pit;
  606. pit->kvm = kvm;
  607. pit_state = &pit->pit_state;
  608. pit_state->pit = pit;
  609. hrtimer_init(&pit_state->pit_timer.timer,
  610. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  611. pit_state->irq_ack_notifier.gsi = 0;
  612. pit_state->irq_ack_notifier.irq_acked = kvm_pit_ack_irq;
  613. kvm_register_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
  614. pit_state->pit_timer.reinject = true;
  615. mutex_unlock(&pit->pit_state.lock);
  616. kvm_pit_reset(pit);
  617. pit->mask_notifier.func = pit_mask_notifer;
  618. kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  619. kvm_iodevice_init(&pit->dev, &pit_dev_ops);
  620. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS,
  621. KVM_PIT_MEM_LENGTH, &pit->dev);
  622. if (ret < 0)
  623. goto fail;
  624. if (flags & KVM_PIT_SPEAKER_DUMMY) {
  625. kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
  626. ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
  627. KVM_SPEAKER_BASE_ADDRESS, 4,
  628. &pit->speaker_dev);
  629. if (ret < 0)
  630. goto fail_unregister;
  631. }
  632. return pit;
  633. fail_unregister:
  634. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &pit->dev);
  635. fail:
  636. kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
  637. kvm_unregister_irq_ack_notifier(kvm, &pit_state->irq_ack_notifier);
  638. kvm_free_irq_source_id(kvm, pit->irq_source_id);
  639. kthread_stop(pit->worker_task);
  640. kfree(pit);
  641. return NULL;
  642. }
  643. void kvm_free_pit(struct kvm *kvm)
  644. {
  645. struct hrtimer *timer;
  646. if (kvm->arch.vpit) {
  647. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &kvm->arch.vpit->dev);
  648. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  649. &kvm->arch.vpit->speaker_dev);
  650. kvm_unregister_irq_mask_notifier(kvm, 0,
  651. &kvm->arch.vpit->mask_notifier);
  652. kvm_unregister_irq_ack_notifier(kvm,
  653. &kvm->arch.vpit->pit_state.irq_ack_notifier);
  654. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  655. timer = &kvm->arch.vpit->pit_state.pit_timer.timer;
  656. hrtimer_cancel(timer);
  657. flush_kthread_work(&kvm->arch.vpit->expired);
  658. kthread_stop(kvm->arch.vpit->worker_task);
  659. kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id);
  660. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  661. kfree(kvm->arch.vpit);
  662. }
  663. }