ioapic.c 11 KB

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