ioapic.c 11 KB

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