ioapic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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_deliver(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 __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
  89. {
  90. bool new_val, old_val;
  91. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  92. union kvm_ioapic_redirect_entry *e;
  93. e = &ioapic->redirtbl[RTC_GSI];
  94. if (!kvm_apic_match_dest(vcpu, NULL, 0, e->fields.dest_id,
  95. e->fields.dest_mode))
  96. return;
  97. new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector);
  98. old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
  99. if (new_val == old_val)
  100. return;
  101. if (new_val) {
  102. __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
  103. ioapic->rtc_status.pending_eoi++;
  104. } else {
  105. __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
  106. ioapic->rtc_status.pending_eoi--;
  107. }
  108. WARN_ON(ioapic->rtc_status.pending_eoi < 0);
  109. }
  110. void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
  111. {
  112. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  113. spin_lock(&ioapic->lock);
  114. __rtc_irq_eoi_tracking_restore_one(vcpu);
  115. spin_unlock(&ioapic->lock);
  116. }
  117. static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)
  118. {
  119. struct kvm_vcpu *vcpu;
  120. int i;
  121. if (RTC_GSI >= IOAPIC_NUM_PINS)
  122. return;
  123. rtc_irq_eoi_tracking_reset(ioapic);
  124. kvm_for_each_vcpu(i, vcpu, ioapic->kvm)
  125. __rtc_irq_eoi_tracking_restore_one(vcpu);
  126. }
  127. static void rtc_irq_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu)
  128. {
  129. if (test_and_clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map))
  130. --ioapic->rtc_status.pending_eoi;
  131. WARN_ON(ioapic->rtc_status.pending_eoi < 0);
  132. }
  133. static bool rtc_irq_check_coalesced(struct kvm_ioapic *ioapic)
  134. {
  135. if (ioapic->rtc_status.pending_eoi > 0)
  136. return true; /* coalesced */
  137. return false;
  138. }
  139. static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx,
  140. bool line_status)
  141. {
  142. union kvm_ioapic_redirect_entry *pent;
  143. int injected = -1;
  144. pent = &ioapic->redirtbl[idx];
  145. if (!pent->fields.mask) {
  146. injected = ioapic_deliver(ioapic, idx, line_status);
  147. if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  148. pent->fields.remote_irr = 1;
  149. }
  150. return injected;
  151. }
  152. static void update_handled_vectors(struct kvm_ioapic *ioapic)
  153. {
  154. DECLARE_BITMAP(handled_vectors, 256);
  155. int i;
  156. memset(handled_vectors, 0, sizeof(handled_vectors));
  157. for (i = 0; i < IOAPIC_NUM_PINS; ++i)
  158. __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
  159. memcpy(ioapic->handled_vectors, handled_vectors,
  160. sizeof(handled_vectors));
  161. smp_wmb();
  162. }
  163. void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu,
  164. u64 *eoi_exit_bitmap)
  165. {
  166. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  167. union kvm_ioapic_redirect_entry *e;
  168. int index;
  169. spin_lock(&ioapic->lock);
  170. /* traverse ioapic entry to set eoi exit bitmap*/
  171. for (index = 0; index < IOAPIC_NUM_PINS; index++) {
  172. e = &ioapic->redirtbl[index];
  173. if (!e->fields.mask &&
  174. (e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
  175. kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
  176. index) || index == RTC_GSI)) {
  177. if (kvm_apic_match_dest(vcpu, NULL, 0,
  178. e->fields.dest_id, e->fields.dest_mode))
  179. __set_bit(e->fields.vector, (unsigned long *)eoi_exit_bitmap);
  180. }
  181. }
  182. spin_unlock(&ioapic->lock);
  183. }
  184. EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap);
  185. void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm)
  186. {
  187. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  188. if (!kvm_apic_vid_enabled(kvm) || !ioapic)
  189. return;
  190. kvm_make_update_eoibitmap_request(kvm);
  191. }
  192. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  193. {
  194. unsigned index;
  195. bool mask_before, mask_after;
  196. union kvm_ioapic_redirect_entry *e;
  197. switch (ioapic->ioregsel) {
  198. case IOAPIC_REG_VERSION:
  199. /* Writes are ignored. */
  200. break;
  201. case IOAPIC_REG_APIC_ID:
  202. ioapic->id = (val >> 24) & 0xf;
  203. break;
  204. case IOAPIC_REG_ARB_ID:
  205. break;
  206. default:
  207. index = (ioapic->ioregsel - 0x10) >> 1;
  208. ioapic_debug("change redir index %x val %x\n", index, val);
  209. if (index >= IOAPIC_NUM_PINS)
  210. return;
  211. e = &ioapic->redirtbl[index];
  212. mask_before = e->fields.mask;
  213. if (ioapic->ioregsel & 1) {
  214. e->bits &= 0xffffffff;
  215. e->bits |= (u64) val << 32;
  216. } else {
  217. e->bits &= ~0xffffffffULL;
  218. e->bits |= (u32) val;
  219. e->fields.remote_irr = 0;
  220. }
  221. update_handled_vectors(ioapic);
  222. mask_after = e->fields.mask;
  223. if (mask_before != mask_after)
  224. kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
  225. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
  226. && ioapic->irr & (1 << index))
  227. ioapic_service(ioapic, index, false);
  228. kvm_ioapic_make_eoibitmap_request(ioapic->kvm);
  229. break;
  230. }
  231. }
  232. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq, bool line_status)
  233. {
  234. union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
  235. struct kvm_lapic_irq irqe;
  236. int ret;
  237. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  238. "vector=%x trig_mode=%x\n",
  239. entry->fields.dest_id, entry->fields.dest_mode,
  240. entry->fields.delivery_mode, entry->fields.vector,
  241. entry->fields.trig_mode);
  242. irqe.dest_id = entry->fields.dest_id;
  243. irqe.vector = entry->fields.vector;
  244. irqe.dest_mode = entry->fields.dest_mode;
  245. irqe.trig_mode = entry->fields.trig_mode;
  246. irqe.delivery_mode = entry->fields.delivery_mode << 8;
  247. irqe.level = 1;
  248. irqe.shorthand = 0;
  249. if (irq == RTC_GSI && line_status) {
  250. BUG_ON(ioapic->rtc_status.pending_eoi != 0);
  251. ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe,
  252. ioapic->rtc_status.dest_map);
  253. ioapic->rtc_status.pending_eoi = ret;
  254. } else
  255. ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
  256. return ret;
  257. }
  258. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
  259. int level, bool line_status)
  260. {
  261. u32 old_irr;
  262. u32 mask = 1 << irq;
  263. union kvm_ioapic_redirect_entry entry;
  264. int ret, irq_level;
  265. BUG_ON(irq < 0 || irq >= IOAPIC_NUM_PINS);
  266. spin_lock(&ioapic->lock);
  267. old_irr = ioapic->irr;
  268. irq_level = __kvm_irq_line_state(&ioapic->irq_states[irq],
  269. irq_source_id, level);
  270. entry = ioapic->redirtbl[irq];
  271. irq_level ^= entry.fields.polarity;
  272. if (!irq_level) {
  273. ioapic->irr &= ~mask;
  274. ret = 1;
  275. } else {
  276. int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  277. if (irq == RTC_GSI && line_status &&
  278. rtc_irq_check_coalesced(ioapic)) {
  279. ret = 0; /* coalesced */
  280. goto out;
  281. }
  282. ioapic->irr |= mask;
  283. if ((edge && old_irr != ioapic->irr) ||
  284. (!edge && !entry.fields.remote_irr))
  285. ret = ioapic_service(ioapic, irq, line_status);
  286. else
  287. ret = 0; /* report coalesced interrupt */
  288. }
  289. out:
  290. trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
  291. spin_unlock(&ioapic->lock);
  292. return ret;
  293. }
  294. void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
  295. {
  296. int i;
  297. spin_lock(&ioapic->lock);
  298. for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
  299. __clear_bit(irq_source_id, &ioapic->irq_states[i]);
  300. spin_unlock(&ioapic->lock);
  301. }
  302. static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
  303. struct kvm_ioapic *ioapic, int vector, int trigger_mode)
  304. {
  305. int i;
  306. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  307. union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
  308. if (ent->fields.vector != vector)
  309. continue;
  310. if (i == RTC_GSI)
  311. rtc_irq_eoi(ioapic, vcpu);
  312. /*
  313. * We are dropping lock while calling ack notifiers because ack
  314. * notifier callbacks for assigned devices call into IOAPIC
  315. * recursively. Since remote_irr is cleared only after call
  316. * to notifiers if the same vector will be delivered while lock
  317. * is dropped it will be put into irr and will be delivered
  318. * after ack notifier returns.
  319. */
  320. spin_unlock(&ioapic->lock);
  321. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
  322. spin_lock(&ioapic->lock);
  323. if (trigger_mode != IOAPIC_LEVEL_TRIG)
  324. continue;
  325. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  326. ent->fields.remote_irr = 0;
  327. if (!ent->fields.mask && (ioapic->irr & (1 << i)))
  328. ioapic_service(ioapic, i, false);
  329. }
  330. }
  331. bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
  332. {
  333. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  334. smp_rmb();
  335. return test_bit(vector, ioapic->handled_vectors);
  336. }
  337. void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
  338. {
  339. struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
  340. spin_lock(&ioapic->lock);
  341. __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
  342. spin_unlock(&ioapic->lock);
  343. }
  344. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  345. {
  346. return container_of(dev, struct kvm_ioapic, dev);
  347. }
  348. static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
  349. {
  350. return ((addr >= ioapic->base_address &&
  351. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  352. }
  353. static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  354. void *val)
  355. {
  356. struct kvm_ioapic *ioapic = to_ioapic(this);
  357. u32 result;
  358. if (!ioapic_in_range(ioapic, addr))
  359. return -EOPNOTSUPP;
  360. ioapic_debug("addr %lx\n", (unsigned long)addr);
  361. ASSERT(!(addr & 0xf)); /* check alignment */
  362. addr &= 0xff;
  363. spin_lock(&ioapic->lock);
  364. switch (addr) {
  365. case IOAPIC_REG_SELECT:
  366. result = ioapic->ioregsel;
  367. break;
  368. case IOAPIC_REG_WINDOW:
  369. result = ioapic_read_indirect(ioapic, addr, len);
  370. break;
  371. default:
  372. result = 0;
  373. break;
  374. }
  375. spin_unlock(&ioapic->lock);
  376. switch (len) {
  377. case 8:
  378. *(u64 *) val = result;
  379. break;
  380. case 1:
  381. case 2:
  382. case 4:
  383. memcpy(val, (char *)&result, len);
  384. break;
  385. default:
  386. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  387. }
  388. return 0;
  389. }
  390. static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  391. const void *val)
  392. {
  393. struct kvm_ioapic *ioapic = to_ioapic(this);
  394. u32 data;
  395. if (!ioapic_in_range(ioapic, addr))
  396. return -EOPNOTSUPP;
  397. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  398. (void*)addr, len, val);
  399. ASSERT(!(addr & 0xf)); /* check alignment */
  400. switch (len) {
  401. case 8:
  402. case 4:
  403. data = *(u32 *) val;
  404. break;
  405. case 2:
  406. data = *(u16 *) val;
  407. break;
  408. case 1:
  409. data = *(u8 *) val;
  410. break;
  411. default:
  412. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  413. return 0;
  414. }
  415. addr &= 0xff;
  416. spin_lock(&ioapic->lock);
  417. switch (addr) {
  418. case IOAPIC_REG_SELECT:
  419. ioapic->ioregsel = data & 0xFF; /* 8-bit register */
  420. break;
  421. case IOAPIC_REG_WINDOW:
  422. ioapic_write_indirect(ioapic, data);
  423. break;
  424. #ifdef CONFIG_IA64
  425. case IOAPIC_REG_EOI:
  426. __kvm_ioapic_update_eoi(NULL, ioapic, data, IOAPIC_LEVEL_TRIG);
  427. break;
  428. #endif
  429. default:
  430. break;
  431. }
  432. spin_unlock(&ioapic->lock);
  433. return 0;
  434. }
  435. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  436. {
  437. int i;
  438. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  439. ioapic->redirtbl[i].fields.mask = 1;
  440. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  441. ioapic->ioregsel = 0;
  442. ioapic->irr = 0;
  443. ioapic->id = 0;
  444. rtc_irq_eoi_tracking_reset(ioapic);
  445. update_handled_vectors(ioapic);
  446. }
  447. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  448. .read = ioapic_mmio_read,
  449. .write = ioapic_mmio_write,
  450. };
  451. int kvm_ioapic_init(struct kvm *kvm)
  452. {
  453. struct kvm_ioapic *ioapic;
  454. int ret;
  455. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  456. if (!ioapic)
  457. return -ENOMEM;
  458. spin_lock_init(&ioapic->lock);
  459. kvm->arch.vioapic = ioapic;
  460. kvm_ioapic_reset(ioapic);
  461. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  462. ioapic->kvm = kvm;
  463. mutex_lock(&kvm->slots_lock);
  464. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
  465. IOAPIC_MEM_LENGTH, &ioapic->dev);
  466. mutex_unlock(&kvm->slots_lock);
  467. if (ret < 0) {
  468. kvm->arch.vioapic = NULL;
  469. kfree(ioapic);
  470. }
  471. return ret;
  472. }
  473. void kvm_ioapic_destroy(struct kvm *kvm)
  474. {
  475. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  476. if (ioapic) {
  477. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  478. kvm->arch.vioapic = NULL;
  479. kfree(ioapic);
  480. }
  481. }
  482. int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  483. {
  484. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  485. if (!ioapic)
  486. return -EINVAL;
  487. spin_lock(&ioapic->lock);
  488. memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
  489. spin_unlock(&ioapic->lock);
  490. return 0;
  491. }
  492. int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  493. {
  494. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  495. if (!ioapic)
  496. return -EINVAL;
  497. spin_lock(&ioapic->lock);
  498. memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
  499. update_handled_vectors(ioapic);
  500. kvm_ioapic_make_eoibitmap_request(kvm);
  501. kvm_rtc_eoi_tracking_restore_all(ioapic);
  502. spin_unlock(&ioapic->lock);
  503. return 0;
  504. }