ioapic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (C) 2001 MandrakeSoft S.A.
  3. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  4. *
  5. * MandrakeSoft S.A.
  6. * 43, rue d'Aboukir
  7. * 75002 Paris - France
  8. * http://www.linux-mandrake.com/
  9. * http://www.mandrakesoft.com/
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Yunhong Jiang <yunhong.jiang@intel.com>
  26. * Yaozu (Eddie) Dong <eddie.dong@intel.com>
  27. * Based on Xen 3.1 code.
  28. */
  29. #include <linux/kvm_host.h>
  30. #include <linux/kvm.h>
  31. #include <linux/mm.h>
  32. #include <linux/highmem.h>
  33. #include <linux/smp.h>
  34. #include <linux/hrtimer.h>
  35. #include <linux/io.h>
  36. #include <linux/slab.h>
  37. #include <linux/export.h>
  38. #include <asm/processor.h>
  39. #include <asm/page.h>
  40. #include <asm/current.h>
  41. #include <trace/events/kvm.h>
  42. #include "ioapic.h"
  43. #include "lapic.h"
  44. #include "irq.h"
  45. #if 0
  46. #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
  47. #else
  48. #define ioapic_debug(fmt, arg...)
  49. #endif
  50. static int ioapic_service(struct kvm_ioapic *vioapic, int irq,
  51. bool line_status);
  52. static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
  53. unsigned long addr,
  54. unsigned long length)
  55. {
  56. unsigned long result = 0;
  57. switch (ioapic->ioregsel) {
  58. case IOAPIC_REG_VERSION:
  59. result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
  60. | (IOAPIC_VERSION_ID & 0xff));
  61. break;
  62. case IOAPIC_REG_APIC_ID:
  63. case IOAPIC_REG_ARB_ID:
  64. result = ((ioapic->id & 0xf) << 24);
  65. break;
  66. default:
  67. {
  68. u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
  69. u64 redir_content;
  70. if (redir_index < IOAPIC_NUM_PINS)
  71. redir_content =
  72. ioapic->redirtbl[redir_index].bits;
  73. else
  74. redir_content = ~0ULL;
  75. result = (ioapic->ioregsel & 0x1) ?
  76. (redir_content >> 32) & 0xffffffff :
  77. redir_content & 0xffffffff;
  78. break;
  79. }
  80. }
  81. return result;
  82. }
  83. static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic *ioapic)
  84. {
  85. ioapic->rtc_status.pending_eoi = 0;
  86. bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
  87. }
  88. static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic);
  89. static void rtc_status_pending_eoi_check_valid(struct kvm_ioapic *ioapic)
  90. {
  91. if (WARN_ON(ioapic->rtc_status.pending_eoi < 0))
  92. kvm_rtc_eoi_tracking_restore_all(ioapic);
  93. }
  94. static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
  95. {
  96. bool new_val, old_val;
  97. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  98. union kvm_ioapic_redirect_entry *e;
  99. e = &ioapic->redirtbl[RTC_GSI];
  100. if (!kvm_apic_match_dest(vcpu, NULL, 0, e->fields.dest_id,
  101. e->fields.dest_mode))
  102. return;
  103. new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
  104. old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
  105. if (new_val == old_val)
  106. return;
  107. if (new_val) {
  108. __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
  109. ioapic->rtc_status.pending_eoi++;
  110. } else {
  111. __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
  112. ioapic->rtc_status.pending_eoi--;
  113. rtc_status_pending_eoi_check_valid(ioapic);
  114. }
  115. }
  116. void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
  117. {
  118. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  119. spin_lock(&ioapic->lock);
  120. __rtc_irq_eoi_tracking_restore_one(vcpu);
  121. spin_unlock(&ioapic->lock);
  122. }
  123. static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
  124. {
  125. struct kvm_vcpu *vcpu;
  126. int i;
  127. if (RTC_GSI >= IOAPIC_NUM_PINS)
  128. return;
  129. rtc_irq_eoi_tracking_reset(ioapic);
  130. kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
  131. __rtc_irq_eoi_tracking_restore_one(vcpu);
  132. }
  133. static void rtc_irq_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu)
  134. {
  135. if (test_and_clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map)) {
  136. --ioapic->rtc_status.pending_eoi;
  137. rtc_status_pending_eoi_check_valid(ioapic);
  138. }
  139. }
  140. static bool rtc_irq_check_coalesced(struct kvm_ioapic *ioapic)
  141. {
  142. if (ioapic->rtc_status.pending_eoi > 0)
  143. return true; /* coalesced */
  144. return false;
  145. }
  146. static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq,
  147. int irq_level, bool line_status)
  148. {
  149. union kvm_ioapic_redirect_entry entry;
  150. u32 mask = 1 << irq;
  151. u32 old_irr;
  152. int edge, ret;
  153. entry = ioapic->redirtbl[irq];
  154. edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  155. if (!irq_level) {
  156. ioapic->irr &= ~mask;
  157. ret = 1;
  158. goto out;
  159. }
  160. /*
  161. * Return 0 for coalesced interrupts; for edge-triggered interrupts,
  162. * this only happens if a previous edge has not been delivered due
  163. * do masking. For level interrupts, the remote_irr field tells
  164. * us if the interrupt is waiting for an EOI.
  165. *
  166. * RTC is special: it is edge-triggered, but userspace likes to know
  167. * if it has been already ack-ed via EOI because coalesced RTC
  168. * interrupts lead to time drift in Windows guests. So we track
  169. * EOI manually for the RTC interrupt.
  170. */
  171. if (irq == RTC_GSI && line_status &&
  172. rtc_irq_check_coalesced(ioapic)) {
  173. ret = 0;
  174. goto out;
  175. }
  176. old_irr = ioapic->irr;
  177. ioapic->irr |= mask;
  178. if ((edge && old_irr == ioapic->irr) ||
  179. (!edge && entry.fields.remote_irr)) {
  180. ret = 0;
  181. goto out;
  182. }
  183. ret = ioapic_service(ioapic, irq, line_status);
  184. out:
  185. trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
  186. return ret;
  187. }
  188. static void kvm_ioapic_inject_all(struct kvm_ioapic *ioapic, unsigned long irr)
  189. {
  190. u32 idx;
  191. rtc_irq_eoi_tracking_reset(ioapic);
  192. for_each_set_bit(idx, &irr, IOAPIC_NUM_PINS)
  193. ioapic_set_irq(ioapic, idx, 1, true);
  194. kvm_rtc_eoi_tracking_restore_all(ioapic);
  195. }
  196. static void update_handled_vectors(struct kvm_ioapic *ioapic)
  197. {
  198. DECLARE_BITMAP(handled_vectors, 256);
  199. int i;
  200. memset(handled_vectors, 0, sizeof(handled_vectors));
  201. for (i = 0; i < IOAPIC_NUM_PINS; ++i)
  202. __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
  203. memcpy(ioapic->handled_vectors, handled_vectors,
  204. sizeof(handled_vectors));
  205. smp_wmb();
  206. }
  207. void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap,
  208. u32 *tmr)
  209. {
  210. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  211. union kvm_ioapic_redirect_entry *e;
  212. int index;
  213. spin_lock(&ioapic->lock);
  214. for (index = 0; index < IOAPIC_NUM_PINS; index++) {
  215. e = &ioapic->redirtbl[index];
  216. if (!e->fields.mask &&
  217. (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
  218. kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
  219. index) || index == RTC_GSI)) {
  220. if (kvm_apic_match_dest(vcpu, NULL, 0,
  221. e->fields.dest_id, e->fields.dest_mode)) {
  222. __set_bit(e->fields.vector,
  223. (unsigned long *)eoi_exit_bitmap);
  224. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  225. __set_bit(e->fields.vector,
  226. (unsigned long *)tmr);
  227. }
  228. }
  229. }
  230. spin_unlock(&ioapic->lock);
  231. }
  232. #ifdef CONFIG_X86
  233. void kvm_vcpu_request_scan_ioapic(struct kvm *kvm)
  234. {
  235. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  236. if (!ioapic)
  237. return;
  238. kvm_make_scan_ioapic_request(kvm);
  239. }
  240. #else
  241. void kvm_vcpu_request_scan_ioapic(struct kvm *kvm)
  242. {
  243. return;
  244. }
  245. #endif
  246. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  247. {
  248. unsigned index;
  249. bool mask_before, mask_after;
  250. union kvm_ioapic_redirect_entry *e;
  251. switch (ioapic->ioregsel) {
  252. case IOAPIC_REG_VERSION:
  253. /* Writes are ignored. */
  254. break;
  255. case IOAPIC_REG_APIC_ID:
  256. ioapic->id = (val >> 24) & 0xf;
  257. break;
  258. case IOAPIC_REG_ARB_ID:
  259. break;
  260. default:
  261. index = (ioapic->ioregsel - 0x10) >> 1;
  262. ioapic_debug("change redir index %x val %x\n", index, val);
  263. if (index >= IOAPIC_NUM_PINS)
  264. return;
  265. e = &ioapic->redirtbl[index];
  266. mask_before = e->fields.mask;
  267. if (ioapic->ioregsel & 1) {
  268. e->bits &= 0xffffffff;
  269. e->bits |= (u64) val << 32;
  270. } else {
  271. e->bits &= ~0xffffffffULL;
  272. e->bits |= (u32) val;
  273. e->fields.remote_irr = 0;
  274. }
  275. update_handled_vectors(ioapic);
  276. mask_after = e->fields.mask;
  277. if (mask_before != mask_after)
  278. kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
  279. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
  280. && ioapic->irr & (1 << index))
  281. ioapic_service(ioapic, index, false);
  282. kvm_vcpu_request_scan_ioapic(ioapic->kvm);
  283. break;
  284. }
  285. }
  286. static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)
  287. {
  288. union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
  289. struct kvm_lapic_irq irqe;
  290. int ret;
  291. if (entry->fields.mask)
  292. return -1;
  293. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  294. "vector=%x trig_mode=%x\n",
  295. entry->fields.dest_id, entry->fields.dest_mode,
  296. entry->fields.delivery_mode, entry->fields.vector,
  297. entry->fields.trig_mode);
  298. irqe.dest_id = entry->fields.dest_id;
  299. irqe.vector = entry->fields.vector;
  300. irqe.dest_mode = entry->fields.dest_mode;
  301. irqe.trig_mode = entry->fields.trig_mode;
  302. irqe.delivery_mode = entry->fields.delivery_mode << 8;
  303. irqe.level = 1;
  304. irqe.shorthand = 0;
  305. if (irqe.trig_mode == IOAPIC_EDGE_TRIG)
  306. ioapic->irr &= ~(1 << irq);
  307. if (irq == RTC_GSI && line_status) {
  308. /*
  309. * pending_eoi cannot ever become negative (see
  310. * rtc_status_pending_eoi_check_valid) and the caller
  311. * ensures that it is only called if it is >= zero, namely
  312. * if rtc_irq_check_coalesced returns false).
  313. */
  314. BUG_ON(ioapic->rtc_status.pending_eoi != 0);
  315. ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe,
  316. ioapic->rtc_status.dest_map);
  317. ioapic->rtc_status.pending_eoi = (ret < 0 ? 0 : ret);
  318. } else
  319. ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
  320. if (ret && irqe.trig_mode == IOAPIC_LEVEL_TRIG)
  321. entry->fields.remote_irr = 1;
  322. return ret;
  323. }
  324. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
  325. int level, bool line_status)
  326. {
  327. int ret, irq_level;
  328. BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
  329. spin_lock(&ioapic->lock);
  330. irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
  331. irq_source_id, level);
  332. ret = ioapic_set_irq(ioapic, irq, irq_level, line_status);
  333. spin_unlock(&ioapic->lock);
  334. return ret;
  335. }
  336. void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
  337. {
  338. int i;
  339. spin_lock(&ioapic->lock);
  340. for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
  341. __clear_bit(irq_source_id, &ioapic->irq_states[i]);
  342. spin_unlock(&ioapic->lock);
  343. }
  344. static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
  345. struct kvm_ioapic *ioapic, int vector, int trigger_mode)
  346. {
  347. int i;
  348. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  349. union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
  350. if (ent->fields.vector != vector)
  351. continue;
  352. if (i == RTC_GSI)
  353. rtc_irq_eoi(ioapic, vcpu);
  354. /*
  355. * We are dropping lock while calling ack notifiers because ack
  356. * notifier callbacks for assigned devices call into IOAPIC
  357. * recursively. Since remote_irr is cleared only after call
  358. * to notifiers if the same vector will be delivered while lock
  359. * is dropped it will be put into irr and will be delivered
  360. * after ack notifier returns.
  361. */
  362. spin_unlock(&ioapic->lock);
  363. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
  364. spin_lock(&ioapic->lock);
  365. if (trigger_mode != IOAPIC_LEVEL_TRIG)
  366. continue;
  367. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  368. ent->fields.remote_irr = 0;
  369. if (ioapic->irr & (1 << i))
  370. ioapic_service(ioapic, i, false);
  371. }
  372. }
  373. bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
  374. {
  375. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  376. smp_rmb();
  377. return test_bit(vector, ioapic->handled_vectors);
  378. }
  379. void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
  380. {
  381. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  382. spin_lock(&ioapic->lock);
  383. __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
  384. spin_unlock(&ioapic->lock);
  385. }
  386. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  387. {
  388. return container_of(dev, struct kvm_ioapic, dev);
  389. }
  390. static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
  391. {
  392. return ((addr >= ioapic->base_address &&
  393. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  394. }
  395. static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  396. void *val)
  397. {
  398. struct kvm_ioapic *ioapic = to_ioapic(this);
  399. u32 result;
  400. if (!ioapic_in_range(ioapic, addr))
  401. return -EOPNOTSUPP;
  402. ioapic_debug("addr %lx\n", (unsigned long)addr);
  403. ASSERT(!(addr & 0xf)); /* check alignment */
  404. addr &= 0xff;
  405. spin_lock(&ioapic->lock);
  406. switch (addr) {
  407. case IOAPIC_REG_SELECT:
  408. result = ioapic->ioregsel;
  409. break;
  410. case IOAPIC_REG_WINDOW:
  411. result = ioapic_read_indirect(ioapic, addr, len);
  412. break;
  413. default:
  414. result = 0;
  415. break;
  416. }
  417. spin_unlock(&ioapic->lock);
  418. switch (len) {
  419. case 8:
  420. *(u64 *) val = result;
  421. break;
  422. case 1:
  423. case 2:
  424. case 4:
  425. memcpy(val, (char *)&result, len);
  426. break;
  427. default:
  428. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  429. }
  430. return 0;
  431. }
  432. static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  433. const void *val)
  434. {
  435. struct kvm_ioapic *ioapic = to_ioapic(this);
  436. u32 data;
  437. if (!ioapic_in_range(ioapic, addr))
  438. return -EOPNOTSUPP;
  439. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  440. (void*)addr, len, val);
  441. ASSERT(!(addr & 0xf)); /* check alignment */
  442. switch (len) {
  443. case 8:
  444. case 4:
  445. data = *(u32 *) val;
  446. break;
  447. case 2:
  448. data = *(u16 *) val;
  449. break;
  450. case 1:
  451. data = *(u8 *) val;
  452. break;
  453. default:
  454. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  455. return 0;
  456. }
  457. addr &= 0xff;
  458. spin_lock(&ioapic->lock);
  459. switch (addr) {
  460. case IOAPIC_REG_SELECT:
  461. ioapic->ioregsel = data & 0xFF; /* 8-bit register */
  462. break;
  463. case IOAPIC_REG_WINDOW:
  464. ioapic_write_indirect(ioapic, data);
  465. break;
  466. #ifdef CONFIG_IA64
  467. case IOAPIC_REG_EOI:
  468. __kvm_ioapic_update_eoi(NULL, ioapic, data, IOAPIC_LEVEL_TRIG);
  469. break;
  470. #endif
  471. default:
  472. break;
  473. }
  474. spin_unlock(&ioapic->lock);
  475. return 0;
  476. }
  477. static void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  478. {
  479. int i;
  480. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  481. ioapic->redirtbl[i].fields.mask = 1;
  482. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  483. ioapic->ioregsel = 0;
  484. ioapic->irr = 0;
  485. ioapic->id = 0;
  486. rtc_irq_eoi_tracking_reset(ioapic);
  487. update_handled_vectors(ioapic);
  488. }
  489. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  490. .read = ioapic_mmio_read,
  491. .write = ioapic_mmio_write,
  492. };
  493. int kvm_ioapic_init(struct kvm *kvm)
  494. {
  495. struct kvm_ioapic *ioapic;
  496. int ret;
  497. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  498. if (!ioapic)
  499. return -ENOMEM;
  500. spin_lock_init(&ioapic->lock);
  501. kvm->arch.vioapic = ioapic;
  502. kvm_ioapic_reset(ioapic);
  503. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  504. ioapic->kvm = kvm;
  505. mutex_lock(&kvm->slots_lock);
  506. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
  507. IOAPIC_MEM_LENGTH, &ioapic->dev);
  508. mutex_unlock(&kvm->slots_lock);
  509. if (ret < 0) {
  510. kvm->arch.vioapic = NULL;
  511. kfree(ioapic);
  512. }
  513. return ret;
  514. }
  515. void kvm_ioapic_destroy(struct kvm *kvm)
  516. {
  517. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  518. if (ioapic) {
  519. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  520. kvm->arch.vioapic = NULL;
  521. kfree(ioapic);
  522. }
  523. }
  524. int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  525. {
  526. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  527. if (!ioapic)
  528. return -EINVAL;
  529. spin_lock(&ioapic->lock);
  530. memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
  531. spin_unlock(&ioapic->lock);
  532. return 0;
  533. }
  534. int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  535. {
  536. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  537. if (!ioapic)
  538. return -EINVAL;
  539. spin_lock(&ioapic->lock);
  540. memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
  541. ioapic->irr = 0;
  542. update_handled_vectors(ioapic);
  543. kvm_vcpu_request_scan_ioapic(kvm);
  544. kvm_ioapic_inject_all(ioapic, state->irr);
  545. spin_unlock(&ioapic->lock);
  546. return 0;
  547. }