ioapic.c 18 KB

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