ioapic.c 15 KB

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