ioapic.c 11 KB

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