ioapic.c 12 KB

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