ioapic.c 15 KB

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