ioapic.c 14 KB

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