ioapic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #ifdef CONFIG_X86
  157. /* Always delivery PIT interrupt to vcpu 0 */
  158. if (irq == 0) {
  159. irqe.dest_mode = 0; /* Physical mode. */
  160. /* need to read apic_id from apic regiest since
  161. * it can be rewritten */
  162. irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id;
  163. }
  164. #endif
  165. return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
  166. }
  167. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  168. {
  169. u32 old_irr;
  170. u32 mask = 1 << irq;
  171. union kvm_ioapic_redirect_entry entry;
  172. int ret = 1;
  173. spin_lock(&ioapic->lock);
  174. old_irr = ioapic->irr;
  175. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  176. entry = ioapic->redirtbl[irq];
  177. level ^= entry.fields.polarity;
  178. if (!level)
  179. ioapic->irr &= ~mask;
  180. else {
  181. int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  182. ioapic->irr |= mask;
  183. if ((edge && old_irr != ioapic->irr) ||
  184. (!edge && !entry.fields.remote_irr))
  185. ret = ioapic_service(ioapic, irq);
  186. else
  187. ret = 0; /* report coalesced interrupt */
  188. }
  189. trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
  190. }
  191. spin_unlock(&ioapic->lock);
  192. return ret;
  193. }
  194. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
  195. int trigger_mode)
  196. {
  197. int i;
  198. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  199. union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
  200. if (ent->fields.vector != vector)
  201. continue;
  202. /*
  203. * We are dropping lock while calling ack notifiers because ack
  204. * notifier callbacks for assigned devices call into IOAPIC
  205. * recursively. Since remote_irr is cleared only after call
  206. * to notifiers if the same vector will be delivered while lock
  207. * is dropped it will be put into irr and will be delivered
  208. * after ack notifier returns.
  209. */
  210. spin_unlock(&ioapic->lock);
  211. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
  212. spin_lock(&ioapic->lock);
  213. if (trigger_mode != IOAPIC_LEVEL_TRIG)
  214. continue;
  215. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  216. ent->fields.remote_irr = 0;
  217. if (!ent->fields.mask && (ioapic->irr & (1 << i)))
  218. ioapic_service(ioapic, i);
  219. }
  220. }
  221. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
  222. {
  223. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  224. smp_rmb();
  225. if (!test_bit(vector, ioapic->handled_vectors))
  226. return;
  227. spin_lock(&ioapic->lock);
  228. __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
  229. spin_unlock(&ioapic->lock);
  230. }
  231. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  232. {
  233. return container_of(dev, struct kvm_ioapic, dev);
  234. }
  235. static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
  236. {
  237. return ((addr >= ioapic->base_address &&
  238. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  239. }
  240. static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  241. void *val)
  242. {
  243. struct kvm_ioapic *ioapic = to_ioapic(this);
  244. u32 result;
  245. if (!ioapic_in_range(ioapic, addr))
  246. return -EOPNOTSUPP;
  247. ioapic_debug("addr %lx\n", (unsigned long)addr);
  248. ASSERT(!(addr & 0xf)); /* check alignment */
  249. addr &= 0xff;
  250. spin_lock(&ioapic->lock);
  251. switch (addr) {
  252. case IOAPIC_REG_SELECT:
  253. result = ioapic->ioregsel;
  254. break;
  255. case IOAPIC_REG_WINDOW:
  256. result = ioapic_read_indirect(ioapic, addr, len);
  257. break;
  258. default:
  259. result = 0;
  260. break;
  261. }
  262. spin_unlock(&ioapic->lock);
  263. switch (len) {
  264. case 8:
  265. *(u64 *) val = result;
  266. break;
  267. case 1:
  268. case 2:
  269. case 4:
  270. memcpy(val, (char *)&result, len);
  271. break;
  272. default:
  273. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  274. }
  275. return 0;
  276. }
  277. static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  278. const void *val)
  279. {
  280. struct kvm_ioapic *ioapic = to_ioapic(this);
  281. u32 data;
  282. if (!ioapic_in_range(ioapic, addr))
  283. return -EOPNOTSUPP;
  284. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  285. (void*)addr, len, val);
  286. ASSERT(!(addr & 0xf)); /* check alignment */
  287. if (len == 4 || len == 8)
  288. data = *(u32 *) val;
  289. else {
  290. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  291. return 0;
  292. }
  293. addr &= 0xff;
  294. spin_lock(&ioapic->lock);
  295. switch (addr) {
  296. case IOAPIC_REG_SELECT:
  297. ioapic->ioregsel = data;
  298. break;
  299. case IOAPIC_REG_WINDOW:
  300. ioapic_write_indirect(ioapic, data);
  301. break;
  302. #ifdef CONFIG_IA64
  303. case IOAPIC_REG_EOI:
  304. __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
  305. break;
  306. #endif
  307. default:
  308. break;
  309. }
  310. spin_unlock(&ioapic->lock);
  311. return 0;
  312. }
  313. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  314. {
  315. int i;
  316. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  317. ioapic->redirtbl[i].fields.mask = 1;
  318. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  319. ioapic->ioregsel = 0;
  320. ioapic->irr = 0;
  321. ioapic->id = 0;
  322. update_handled_vectors(ioapic);
  323. }
  324. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  325. .read = ioapic_mmio_read,
  326. .write = ioapic_mmio_write,
  327. };
  328. int kvm_ioapic_init(struct kvm *kvm)
  329. {
  330. struct kvm_ioapic *ioapic;
  331. int ret;
  332. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  333. if (!ioapic)
  334. return -ENOMEM;
  335. spin_lock_init(&ioapic->lock);
  336. kvm->arch.vioapic = ioapic;
  337. kvm_ioapic_reset(ioapic);
  338. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  339. ioapic->kvm = kvm;
  340. mutex_lock(&kvm->slots_lock);
  341. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  342. mutex_unlock(&kvm->slots_lock);
  343. if (ret < 0) {
  344. kvm->arch.vioapic = NULL;
  345. kfree(ioapic);
  346. }
  347. return ret;
  348. }
  349. void kvm_ioapic_destroy(struct kvm *kvm)
  350. {
  351. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  352. if (ioapic) {
  353. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
  354. kvm->arch.vioapic = NULL;
  355. kfree(ioapic);
  356. }
  357. }
  358. int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  359. {
  360. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  361. if (!ioapic)
  362. return -EINVAL;
  363. spin_lock(&ioapic->lock);
  364. memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
  365. spin_unlock(&ioapic->lock);
  366. return 0;
  367. }
  368. int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
  369. {
  370. struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  371. if (!ioapic)
  372. return -EINVAL;
  373. spin_lock(&ioapic->lock);
  374. memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
  375. update_handled_vectors(ioapic);
  376. spin_unlock(&ioapic->lock);
  377. return 0;
  378. }