ioapic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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.map, KVM_MAX_VCPU_ID);
  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. struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
  99. union kvm_ioapic_redirect_entry *e;
  100. e = &ioapic->redirtbl[RTC_GSI];
  101. if (!kvm_apic_match_dest(vcpu, NULL, 0, e->fields.dest_id,
  102. e->fields.dest_mode))
  103. return;
  104. new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
  105. old_val = test_bit(vcpu->vcpu_id, dest_map->map);
  106. if (new_val == old_val)
  107. return;
  108. if (new_val) {
  109. __set_bit(vcpu->vcpu_id, dest_map->map);
  110. dest_map->vectors[vcpu->vcpu_id] = e->fields.vector;
  111. ioapic->rtc_status.pending_eoi++;
  112. } else {
  113. __clear_bit(vcpu->vcpu_id, dest_map->map);
  114. ioapic->rtc_status.pending_eoi--;
  115. rtc_status_pending_eoi_check_valid(ioapic);
  116. }
  117. }
  118. void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
  119. {
  120. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  121. spin_lock(&ioapic->lock);
  122. __rtc_irq_eoi_tracking_restore_one(vcpu);
  123. spin_unlock(&ioapic->lock);
  124. }
  125. static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
  126. {
  127. struct kvm_vcpu *vcpu;
  128. int i;
  129. if (RTC_GSI >= IOAPIC_NUM_PINS)
  130. return;
  131. rtc_irq_eoi_tracking_reset(ioapic);
  132. kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
  133. __rtc_irq_eoi_tracking_restore_one(vcpu);
  134. }
  135. static void rtc_irq_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu)
  136. {
  137. if (test_and_clear_bit(vcpu->vcpu_id,
  138. ioapic->rtc_status.dest_map.map)) {
  139. --ioapic->rtc_status.pending_eoi;
  140. rtc_status_pending_eoi_check_valid(ioapic);
  141. }
  142. }
  143. static bool rtc_irq_check_coalesced(struct kvm_ioapic *ioapic)
  144. {
  145. if (ioapic->rtc_status.pending_eoi > 0)
  146. return true; /* coalesced */
  147. return false;
  148. }
  149. static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq,
  150. int irq_level, bool line_status)
  151. {
  152. union kvm_ioapic_redirect_entry entry;
  153. u32 mask = 1 << irq;
  154. u32 old_irr;
  155. int edge, ret;
  156. entry = ioapic->redirtbl[irq];
  157. edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  158. if (!irq_level) {
  159. ioapic->irr &= ~mask;
  160. ret = 1;
  161. goto out;
  162. }
  163. /*
  164. * Return 0 for coalesced interrupts; for edge-triggered interrupts,
  165. * this only happens if a previous edge has not been delivered due
  166. * do masking. For level interrupts, the remote_irr field tells
  167. * us if the interrupt is waiting for an EOI.
  168. *
  169. * RTC is special: it is edge-triggered, but userspace likes to know
  170. * if it has been already ack-ed via EOI because coalesced RTC
  171. * interrupts lead to time drift in Windows guests. So we track
  172. * EOI manually for the RTC interrupt.
  173. */
  174. if (irq == RTC_GSI && line_status &&
  175. rtc_irq_check_coalesced(ioapic)) {
  176. ret = 0;
  177. goto out;
  178. }
  179. old_irr = ioapic->irr;
  180. ioapic->irr |= mask;
  181. if (edge) {
  182. ioapic->irr_delivered &= ~mask;
  183. if (old_irr == ioapic->irr) {
  184. ret = 0;
  185. goto out;
  186. }
  187. }
  188. ret = ioapic_service(ioapic, irq, line_status);
  189. out:
  190. trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
  191. return ret;
  192. }
  193. static void kvm_ioapic_inject_all(struct kvm_ioapic *ioapic, unsigned long irr)
  194. {
  195. u32 idx;
  196. rtc_irq_eoi_tracking_reset(ioapic);
  197. for_each_set_bit(idx, &irr, IOAPIC_NUM_PINS)
  198. ioapic_set_irq(ioapic, idx, 1, true);
  199. kvm_rtc_eoi_tracking_restore_all(ioapic);
  200. }
  201. void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, ulong *ioapic_handled_vectors)
  202. {
  203. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  204. struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
  205. union kvm_ioapic_redirect_entry *e;
  206. int index;
  207. spin_lock(&ioapic->lock);
  208. /* Make sure we see any missing RTC EOI */
  209. if (test_bit(vcpu->vcpu_id, dest_map->map))
  210. __set_bit(dest_map->vectors[vcpu->vcpu_id],
  211. ioapic_handled_vectors);
  212. for (index = 0; index < IOAPIC_NUM_PINS; index++) {
  213. e = &ioapic->redirtbl[index];
  214. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
  215. kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index) ||
  216. index == RTC_GSI) {
  217. if (kvm_apic_match_dest(vcpu, NULL, 0,
  218. e->fields.dest_id, e->fields.dest_mode) ||
  219. kvm_apic_pending_eoi(vcpu, e->fields.vector))
  220. __set_bit(e->fields.vector,
  221. ioapic_handled_vectors);
  222. }
  223. }
  224. spin_unlock(&ioapic->lock);
  225. }
  226. void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm)
  227. {
  228. if (!ioapic_in_kernel(kvm))
  229. return;
  230. kvm_make_scan_ioapic_request(kvm);
  231. }
  232. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  233. {
  234. unsigned index;
  235. bool mask_before, mask_after;
  236. int old_remote_irr, old_delivery_status;
  237. union kvm_ioapic_redirect_entry *e;
  238. switch (ioapic->ioregsel) {
  239. case IOAPIC_REG_VERSION:
  240. /* Writes are ignored. */
  241. break;
  242. case IOAPIC_REG_APIC_ID:
  243. ioapic->id = (val >> 24) & 0xf;
  244. break;
  245. case IOAPIC_REG_ARB_ID:
  246. break;
  247. default:
  248. index = (ioapic->ioregsel - 0x10) >> 1;
  249. ioapic_debug("change redir index %x val %x\n", index, val);
  250. if (index >= IOAPIC_NUM_PINS)
  251. return;
  252. e = &ioapic->redirtbl[index];
  253. mask_before = e->fields.mask;
  254. /* Preserve read-only fields */
  255. old_remote_irr = e->fields.remote_irr;
  256. old_delivery_status = e->fields.delivery_status;
  257. if (ioapic->ioregsel & 1) {
  258. e->bits &= 0xffffffff;
  259. e->bits |= (u64) val << 32;
  260. } else {
  261. e->bits &= ~0xffffffffULL;
  262. e->bits |= (u32) val;
  263. }
  264. e->fields.remote_irr = old_remote_irr;
  265. e->fields.delivery_status = old_delivery_status;
  266. /*
  267. * Some OSes (Linux, Xen) assume that Remote IRR bit will
  268. * be cleared by IOAPIC hardware when the entry is configured
  269. * as edge-triggered. This behavior is used to simulate an
  270. * explicit EOI on IOAPICs that don't have the EOI register.
  271. */
  272. if (e->fields.trig_mode == IOAPIC_EDGE_TRIG)
  273. e->fields.remote_irr = 0;
  274. mask_after = e->fields.mask;
  275. if (mask_before != mask_after)
  276. kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
  277. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
  278. && ioapic->irr & (1 << index))
  279. ioapic_service(ioapic, index, false);
  280. kvm_make_scan_ioapic_request(ioapic->kvm);
  281. break;
  282. }
  283. }
  284. static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)
  285. {
  286. union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
  287. struct kvm_lapic_irq irqe;
  288. int ret;
  289. if (entry->fields.mask ||
  290. (entry->fields.trig_mode == IOAPIC_LEVEL_TRIG &&
  291. entry->fields.remote_irr))
  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. irqe.msi_redir_hint = false;
  306. if (irqe.trig_mode == IOAPIC_EDGE_TRIG)
  307. ioapic->irr_delivered |= 1 << irq;
  308. if (irq == RTC_GSI && line_status) {
  309. /*
  310. * pending_eoi cannot ever become negative (see
  311. * rtc_status_pending_eoi_check_valid) and the caller
  312. * ensures that it is only called if it is >= zero, namely
  313. * if rtc_irq_check_coalesced returns false).
  314. */
  315. BUG_ON(ioapic->rtc_status.pending_eoi != 0);
  316. ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe,
  317. &ioapic->rtc_status.dest_map);
  318. ioapic->rtc_status.pending_eoi = (ret < 0 ? 0 : ret);
  319. } else
  320. ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
  321. if (ret && irqe.trig_mode == IOAPIC_LEVEL_TRIG)
  322. entry->fields.remote_irr = 1;
  323. return ret;
  324. }
  325. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
  326. int level, bool line_status)
  327. {
  328. int ret, irq_level;
  329. BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
  330. spin_lock(&ioapic->lock);
  331. irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
  332. irq_source_id, level);
  333. ret = ioapic_set_irq(ioapic, irq, irq_level, line_status);
  334. spin_unlock(&ioapic->lock);
  335. return ret;
  336. }
  337. void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
  338. {
  339. int i;
  340. spin_lock(&ioapic->lock);
  341. for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
  342. __clear_bit(irq_source_id, &ioapic->irq_states[i]);
  343. spin_unlock(&ioapic->lock);
  344. }
  345. static void kvm_ioapic_eoi_inject_work(struct work_struct *work)
  346. {
  347. int i;
  348. struct kvm_ioapic *ioapic = container_of(work, struct kvm_ioapic,
  349. eoi_inject.work);
  350. spin_lock(&ioapic->lock);
  351. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  352. union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
  353. if (ent->fields.trig_mode != IOAPIC_LEVEL_TRIG)
  354. continue;
  355. if (ioapic->irr & (1 << i) && !ent->fields.remote_irr)
  356. ioapic_service(ioapic, i, false);
  357. }
  358. spin_unlock(&ioapic->lock);
  359. }
  360. #define IOAPIC_SUCCESSIVE_IRQ_MAX_COUNT 10000
  361. static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
  362. struct kvm_ioapic *ioapic, int vector, int trigger_mode)
  363. {
  364. struct dest_map *dest_map = &ioapic->rtc_status.dest_map;
  365. struct kvm_lapic *apic = vcpu->arch.apic;
  366. int i;
  367. /* RTC special handling */
  368. if (test_bit(vcpu->vcpu_id, dest_map->map) &&
  369. vector == dest_map->vectors[vcpu->vcpu_id])
  370. rtc_irq_eoi(ioapic, vcpu);
  371. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  372. union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
  373. if (ent->fields.vector != vector)
  374. continue;
  375. /*
  376. * We are dropping lock while calling ack notifiers because ack
  377. * notifier callbacks for assigned devices call into IOAPIC
  378. * recursively. Since remote_irr is cleared only after call
  379. * to notifiers if the same vector will be delivered while lock
  380. * is dropped it will be put into irr and will be delivered
  381. * after ack notifier returns.
  382. */
  383. spin_unlock(&ioapic->lock);
  384. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
  385. spin_lock(&ioapic->lock);
  386. if (trigger_mode != IOAPIC_LEVEL_TRIG ||
  387. kvm_lapic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)
  388. continue;
  389. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  390. ent->fields.remote_irr = 0;
  391. if (!ent->fields.mask && (ioapic->irr & (1 << i))) {
  392. ++ioapic->irq_eoi[i];
  393. if (ioapic->irq_eoi[i] == IOAPIC_SUCCESSIVE_IRQ_MAX_COUNT) {
  394. /*
  395. * Real hardware does not deliver the interrupt
  396. * immediately during eoi broadcast, and this
  397. * lets a buggy guest make slow progress
  398. * even if it does not correctly handle a
  399. * level-triggered interrupt. Emulate this
  400. * behavior if we detect an interrupt storm.
  401. */
  402. schedule_delayed_work(&ioapic->eoi_inject, HZ / 100);
  403. ioapic->irq_eoi[i] = 0;
  404. trace_kvm_ioapic_delayed_eoi_inj(ent->bits);
  405. } else {
  406. ioapic_service(ioapic, i, false);
  407. }
  408. } else {
  409. ioapic->irq_eoi[i] = 0;
  410. }
  411. }
  412. }
  413. void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
  414. {
  415. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  416. spin_lock(&ioapic->lock);
  417. __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
  418. spin_unlock(&ioapic->lock);
  419. }
  420. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  421. {
  422. return container_of(dev, struct kvm_ioapic, dev);
  423. }
  424. static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
  425. {
  426. return ((addr >= ioapic->base_address &&
  427. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  428. }
  429. static int ioapic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
  430. gpa_t addr, int len, void *val)
  431. {
  432. struct kvm_ioapic *ioapic = to_ioapic(this);
  433. u32 result;
  434. if (!ioapic_in_range(ioapic, addr))
  435. return -EOPNOTSUPP;
  436. ioapic_debug("addr %lx\n", (unsigned long)addr);
  437. ASSERT(!(addr & 0xf)); /* check alignment */
  438. addr &= 0xff;
  439. spin_lock(&ioapic->lock);
  440. switch (addr) {
  441. case IOAPIC_REG_SELECT:
  442. result = ioapic->ioregsel;
  443. break;
  444. case IOAPIC_REG_WINDOW:
  445. result = ioapic_read_indirect(ioapic, addr, len);
  446. break;
  447. default:
  448. result = 0;
  449. break;
  450. }
  451. spin_unlock(&ioapic->lock);
  452. switch (len) {
  453. case 8:
  454. *(u64 *) val = result;
  455. break;
  456. case 1:
  457. case 2:
  458. case 4:
  459. memcpy(val, (char *)&result, len);
  460. break;
  461. default:
  462. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  463. }
  464. return 0;
  465. }
  466. static int ioapic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
  467. gpa_t addr, int len, const void *val)
  468. {
  469. struct kvm_ioapic *ioapic = to_ioapic(this);
  470. u32 data;
  471. if (!ioapic_in_range(ioapic, addr))
  472. return -EOPNOTSUPP;
  473. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  474. (void*)addr, len, val);
  475. ASSERT(!(addr & 0xf)); /* check alignment */
  476. switch (len) {
  477. case 8:
  478. case 4:
  479. data = *(u32 *) val;
  480. break;
  481. case 2:
  482. data = *(u16 *) val;
  483. break;
  484. case 1:
  485. data = *(u8 *) val;
  486. break;
  487. default:
  488. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  489. return 0;
  490. }
  491. addr &= 0xff;
  492. spin_lock(&ioapic->lock);
  493. switch (addr) {
  494. case IOAPIC_REG_SELECT:
  495. ioapic->ioregsel = data & 0xFF; /* 8-bit register */
  496. break;
  497. case IOAPIC_REG_WINDOW:
  498. ioapic_write_indirect(ioapic, data);
  499. break;
  500. default:
  501. break;
  502. }
  503. spin_unlock(&ioapic->lock);
  504. return 0;
  505. }
  506. static void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  507. {
  508. int i;
  509. cancel_delayed_work_sync(&ioapic->eoi_inject);
  510. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  511. ioapic->redirtbl[i].fields.mask = 1;
  512. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  513. ioapic->ioregsel = 0;
  514. ioapic->irr = 0;
  515. ioapic->irr_delivered = 0;
  516. ioapic->id = 0;
  517. memset(ioapic->irq_eoi, 0x00, sizeof(ioapic->irq_eoi));
  518. rtc_irq_eoi_tracking_reset(ioapic);
  519. }
  520. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  521. .read = ioapic_mmio_read,
  522. .write = ioapic_mmio_write,
  523. };
  524. int kvm_ioapic_init(struct kvm *kvm)
  525. {
  526. struct kvm_ioapic *ioapic;
  527. int ret;
  528. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  529. if (!ioapic)
  530. return -ENOMEM;
  531. spin_lock_init(&ioapic->lock);
  532. INIT_DELAYED_WORK(&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work);
  533. kvm->arch.vioapic = ioapic;
  534. kvm_ioapic_reset(ioapic);
  535. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  536. ioapic->kvm = kvm;
  537. mutex_lock(&kvm->slots_lock);
  538. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
  539. IOAPIC_MEM_LENGTH, &ioapic->dev);
  540. mutex_unlock(&kvm->slots_lock);
  541. if (ret < 0) {
  542. kvm->arch.vioapic = NULL;
  543. kfree(ioapic);
  544. }
  545. return ret;
  546. }
  547. void kvm_ioapic_destroy(struct kvm *kvm)
  548. {
  549. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  550. if (!ioapic)
  551. return;
  552. cancel_delayed_work_sync(&ioapic->eoi_inject);
  553. mutex_lock(&kvm->slots_lock);
  554. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  555. mutex_unlock(&kvm->slots_lock);
  556. kvm->arch.vioapic = NULL;
  557. kfree(ioapic);
  558. }
  559. void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  560. {
  561. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  562. spin_lock(&ioapic->lock);
  563. memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
  564. state->irr &= ~ioapic->irr_delivered;
  565. spin_unlock(&ioapic->lock);
  566. }
  567. void kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  568. {
  569. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  570. spin_lock(&ioapic->lock);
  571. memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
  572. ioapic->irr = 0;
  573. ioapic->irr_delivered = 0;
  574. kvm_make_scan_ioapic_request(kvm);
  575. kvm_ioapic_inject_all(ioapic, state->irr);
  576. spin_unlock(&ioapic->lock);
  577. }