ioapic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (C) 2001 MandrakeSoft S.A.
  3. *
  4. * MandrakeSoft S.A.
  5. * 43, rue d'Aboukir
  6. * 75002 Paris - France
  7. * http://www.linux-mandrake.com/
  8. * http://www.mandrakesoft.com/
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Yunhong Jiang <yunhong.jiang@intel.com>
  25. * Yaozu (Eddie) Dong <eddie.dong@intel.com>
  26. * Based on Xen 3.1 code.
  27. */
  28. #include <linux/kvm_host.h>
  29. #include <linux/kvm.h>
  30. #include <linux/mm.h>
  31. #include <linux/highmem.h>
  32. #include <linux/smp.h>
  33. #include <linux/hrtimer.h>
  34. #include <linux/io.h>
  35. #include <linux/slab.h>
  36. #include <asm/processor.h>
  37. #include <asm/page.h>
  38. #include <asm/current.h>
  39. #include <trace/events/kvm.h>
  40. #include "ioapic.h"
  41. #include "lapic.h"
  42. #include "irq.h"
  43. #if 0
  44. #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
  45. #else
  46. #define ioapic_debug(fmt, arg...)
  47. #endif
  48. static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
  49. static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
  50. unsigned long addr,
  51. unsigned long length)
  52. {
  53. unsigned long result = 0;
  54. switch (ioapic->ioregsel) {
  55. case IOAPIC_REG_VERSION:
  56. result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
  57. | (IOAPIC_VERSION_ID & 0xff));
  58. break;
  59. case IOAPIC_REG_APIC_ID:
  60. case IOAPIC_REG_ARB_ID:
  61. result = ((ioapic->id & 0xf) << 24);
  62. break;
  63. default:
  64. {
  65. u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
  66. u64 redir_content;
  67. ASSERT(redir_index < IOAPIC_NUM_PINS);
  68. redir_content = ioapic->redirtbl[redir_index].bits;
  69. result = (ioapic->ioregsel & 0x1) ?
  70. (redir_content >> 32) & 0xffffffff :
  71. redir_content & 0xffffffff;
  72. break;
  73. }
  74. }
  75. return result;
  76. }
  77. static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
  78. {
  79. union kvm_ioapic_redirect_entry *pent;
  80. int injected = -1;
  81. pent = &ioapic->redirtbl[idx];
  82. if (!pent->fields.mask) {
  83. injected = ioapic_deliver(ioapic, idx);
  84. if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  85. pent->fields.remote_irr = 1;
  86. }
  87. return injected;
  88. }
  89. static void update_handled_vectors(struct kvm_ioapic *ioapic)
  90. {
  91. DECLARE_BITMAP(handled_vectors, 256);
  92. int i;
  93. memset(handled_vectors, 0, sizeof(handled_vectors));
  94. for (i = 0; i < IOAPIC_NUM_PINS; ++i)
  95. __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
  96. memcpy(ioapic->handled_vectors, handled_vectors,
  97. sizeof(handled_vectors));
  98. smp_wmb();
  99. }
  100. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  101. {
  102. unsigned index;
  103. bool mask_before, mask_after;
  104. union kvm_ioapic_redirect_entry *e;
  105. switch (ioapic->ioregsel) {
  106. case IOAPIC_REG_VERSION:
  107. /* Writes are ignored. */
  108. break;
  109. case IOAPIC_REG_APIC_ID:
  110. ioapic->id = (val >> 24) & 0xf;
  111. break;
  112. case IOAPIC_REG_ARB_ID:
  113. break;
  114. default:
  115. index = (ioapic->ioregsel - 0x10) >> 1;
  116. ioapic_debug("change redir index %x val %x\n", index, val);
  117. if (index >= IOAPIC_NUM_PINS)
  118. return;
  119. e = &ioapic->redirtbl[index];
  120. mask_before = e->fields.mask;
  121. if (ioapic->ioregsel & 1) {
  122. e->bits &= 0xffffffff;
  123. e->bits |= (u64) val << 32;
  124. } else {
  125. e->bits &= ~0xffffffffULL;
  126. e->bits |= (u32) val;
  127. e->fields.remote_irr = 0;
  128. }
  129. update_handled_vectors(ioapic);
  130. mask_after = e->fields.mask;
  131. if (mask_before != mask_after)
  132. kvm_fire_mask_notifiers(ioapic->kvm, index, mask_after);
  133. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
  134. && ioapic->irr & (1 << index))
  135. ioapic_service(ioapic, index);
  136. break;
  137. }
  138. }
  139. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  140. {
  141. union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
  142. struct kvm_lapic_irq irqe;
  143. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  144. "vector=%x trig_mode=%x\n",
  145. entry->fields.dest, entry->fields.dest_mode,
  146. entry->fields.delivery_mode, entry->fields.vector,
  147. entry->fields.trig_mode);
  148. irqe.dest_id = entry->fields.dest_id;
  149. irqe.vector = entry->fields.vector;
  150. irqe.dest_mode = entry->fields.dest_mode;
  151. irqe.trig_mode = entry->fields.trig_mode;
  152. irqe.delivery_mode = entry->fields.delivery_mode << 8;
  153. irqe.level = 1;
  154. irqe.shorthand = 0;
  155. #ifdef CONFIG_X86
  156. /* Always delivery PIT interrupt to vcpu 0 */
  157. if (irq == 0) {
  158. irqe.dest_mode = 0; /* Physical mode. */
  159. /* need to read apic_id from apic regiest since
  160. * it can be rewritten */
  161. irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id;
  162. }
  163. #endif
  164. return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
  165. }
  166. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  167. {
  168. u32 old_irr;
  169. u32 mask = 1 << irq;
  170. union kvm_ioapic_redirect_entry entry;
  171. int ret = 1;
  172. spin_lock(&ioapic->lock);
  173. old_irr = ioapic->irr;
  174. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  175. entry = ioapic->redirtbl[irq];
  176. level ^= entry.fields.polarity;
  177. if (!level)
  178. ioapic->irr &= ~mask;
  179. else {
  180. int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  181. ioapic->irr |= mask;
  182. if ((edge && old_irr != ioapic->irr) ||
  183. (!edge && !entry.fields.remote_irr))
  184. ret = ioapic_service(ioapic, irq);
  185. else
  186. ret = 0; /* report coalesced interrupt */
  187. }
  188. trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
  189. }
  190. spin_unlock(&ioapic->lock);
  191. return ret;
  192. }
  193. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
  194. int trigger_mode)
  195. {
  196. int i;
  197. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  198. union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
  199. if (ent->fields.vector != vector)
  200. continue;
  201. /*
  202. * We are dropping lock while calling ack notifiers because ack
  203. * notifier callbacks for assigned devices call into IOAPIC
  204. * recursively. Since remote_irr is cleared only after call
  205. * to notifiers if the same vector will be delivered while lock
  206. * is dropped it will be put into irr and will be delivered
  207. * after ack notifier returns.
  208. */
  209. spin_unlock(&ioapic->lock);
  210. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
  211. spin_lock(&ioapic->lock);
  212. if (trigger_mode != IOAPIC_LEVEL_TRIG)
  213. continue;
  214. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  215. ent->fields.remote_irr = 0;
  216. if (!ent->fields.mask && (ioapic->irr & (1 << i)))
  217. ioapic_service(ioapic, i);
  218. }
  219. }
  220. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
  221. {
  222. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  223. smp_rmb();
  224. if (!test_bit(vector, ioapic->handled_vectors))
  225. return;
  226. spin_lock(&ioapic->lock);
  227. __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
  228. spin_unlock(&ioapic->lock);
  229. }
  230. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  231. {
  232. return container_of(dev, struct kvm_ioapic, dev);
  233. }
  234. static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
  235. {
  236. return ((addr >= ioapic->base_address &&
  237. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  238. }
  239. static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  240. void *val)
  241. {
  242. struct kvm_ioapic *ioapic = to_ioapic(this);
  243. u32 result;
  244. if (!ioapic_in_range(ioapic, addr))
  245. return -EOPNOTSUPP;
  246. ioapic_debug("addr %lx\n", (unsigned long)addr);
  247. ASSERT(!(addr & 0xf)); /* check alignment */
  248. addr &= 0xff;
  249. spin_lock(&ioapic->lock);
  250. switch (addr) {
  251. case IOAPIC_REG_SELECT:
  252. result = ioapic->ioregsel;
  253. break;
  254. case IOAPIC_REG_WINDOW:
  255. result = ioapic_read_indirect(ioapic, addr, len);
  256. break;
  257. default:
  258. result = 0;
  259. break;
  260. }
  261. spin_unlock(&ioapic->lock);
  262. switch (len) {
  263. case 8:
  264. *(u64 *) val = result;
  265. break;
  266. case 1:
  267. case 2:
  268. case 4:
  269. memcpy(val, (char *)&result, len);
  270. break;
  271. default:
  272. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  273. }
  274. return 0;
  275. }
  276. static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  277. const void *val)
  278. {
  279. struct kvm_ioapic *ioapic = to_ioapic(this);
  280. u32 data;
  281. if (!ioapic_in_range(ioapic, addr))
  282. return -EOPNOTSUPP;
  283. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  284. (void*)addr, len, val);
  285. ASSERT(!(addr & 0xf)); /* check alignment */
  286. if (len == 4 || len == 8)
  287. data = *(u32 *) val;
  288. else {
  289. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  290. return 0;
  291. }
  292. addr &= 0xff;
  293. spin_lock(&ioapic->lock);
  294. switch (addr) {
  295. case IOAPIC_REG_SELECT:
  296. ioapic->ioregsel = data;
  297. break;
  298. case IOAPIC_REG_WINDOW:
  299. ioapic_write_indirect(ioapic, data);
  300. break;
  301. #ifdef CONFIG_IA64
  302. case IOAPIC_REG_EOI:
  303. __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
  304. break;
  305. #endif
  306. default:
  307. break;
  308. }
  309. spin_unlock(&ioapic->lock);
  310. return 0;
  311. }
  312. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  313. {
  314. int i;
  315. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  316. ioapic->redirtbl[i].fields.mask = 1;
  317. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  318. ioapic->ioregsel = 0;
  319. ioapic->irr = 0;
  320. ioapic->id = 0;
  321. update_handled_vectors(ioapic);
  322. }
  323. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  324. .read = ioapic_mmio_read,
  325. .write = ioapic_mmio_write,
  326. };
  327. int kvm_ioapic_init(struct kvm *kvm)
  328. {
  329. struct kvm_ioapic *ioapic;
  330. int ret;
  331. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  332. if (!ioapic)
  333. return -ENOMEM;
  334. spin_lock_init(&ioapic->lock);
  335. kvm->arch.vioapic = ioapic;
  336. kvm_ioapic_reset(ioapic);
  337. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  338. ioapic->kvm = kvm;
  339. mutex_lock(&kvm->slots_lock);
  340. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  341. mutex_unlock(&kvm->slots_lock);
  342. if (ret < 0) {
  343. kvm->arch.vioapic = NULL;
  344. kfree(ioapic);
  345. }
  346. return ret;
  347. }
  348. void kvm_ioapic_destroy(struct kvm *kvm)
  349. {
  350. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  351. if (ioapic) {
  352. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  353. kvm->arch.vioapic = NULL;
  354. kfree(ioapic);
  355. }
  356. }
  357. int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  358. {
  359. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  360. if (!ioapic)
  361. return -EINVAL;
  362. spin_lock(&ioapic->lock);
  363. memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
  364. spin_unlock(&ioapic->lock);
  365. return 0;
  366. }
  367. int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  368. {
  369. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  370. if (!ioapic)
  371. return -EINVAL;
  372. spin_lock(&ioapic->lock);
  373. memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
  374. update_handled_vectors(ioapic);
  375. spin_unlock(&ioapic->lock);
  376. return 0;
  377. }