ioapic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 <asm/processor.h>
  36. #include <asm/page.h>
  37. #include <asm/current.h>
  38. #include <trace/events/kvm.h>
  39. #include "ioapic.h"
  40. #include "lapic.h"
  41. #include "irq.h"
  42. #if 0
  43. #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
  44. #else
  45. #define ioapic_debug(fmt, arg...)
  46. #endif
  47. static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
  48. static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
  49. unsigned long addr,
  50. unsigned long length)
  51. {
  52. unsigned long result = 0;
  53. switch (ioapic->ioregsel) {
  54. case IOAPIC_REG_VERSION:
  55. result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
  56. | (IOAPIC_VERSION_ID & 0xff));
  57. break;
  58. case IOAPIC_REG_APIC_ID:
  59. case IOAPIC_REG_ARB_ID:
  60. result = ((ioapic->id & 0xf) << 24);
  61. break;
  62. default:
  63. {
  64. u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
  65. u64 redir_content;
  66. ASSERT(redir_index < IOAPIC_NUM_PINS);
  67. redir_content = ioapic->redirtbl[redir_index].bits;
  68. result = (ioapic->ioregsel & 0x1) ?
  69. (redir_content >> 32) & 0xffffffff :
  70. redir_content & 0xffffffff;
  71. break;
  72. }
  73. }
  74. return result;
  75. }
  76. static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
  77. {
  78. union kvm_ioapic_redirect_entry *pent;
  79. int injected = -1;
  80. pent = &ioapic->redirtbl[idx];
  81. if (!pent->fields.mask) {
  82. injected = ioapic_deliver(ioapic, idx);
  83. if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  84. pent->fields.remote_irr = 1;
  85. }
  86. return injected;
  87. }
  88. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  89. {
  90. unsigned index;
  91. bool mask_before, mask_after;
  92. union kvm_ioapic_redirect_entry *e;
  93. switch (ioapic->ioregsel) {
  94. case IOAPIC_REG_VERSION:
  95. /* Writes are ignored. */
  96. break;
  97. case IOAPIC_REG_APIC_ID:
  98. ioapic->id = (val >> 24) & 0xf;
  99. break;
  100. case IOAPIC_REG_ARB_ID:
  101. break;
  102. default:
  103. index = (ioapic->ioregsel - 0x10) >> 1;
  104. ioapic_debug("change redir index %x val %x\n", index, val);
  105. if (index >= IOAPIC_NUM_PINS)
  106. return;
  107. e = &ioapic->redirtbl[index];
  108. mask_before = e->fields.mask;
  109. if (ioapic->ioregsel & 1) {
  110. e->bits &= 0xffffffff;
  111. e->bits |= (u64) val << 32;
  112. } else {
  113. e->bits &= ~0xffffffffULL;
  114. e->bits |= (u32) val;
  115. e->fields.remote_irr = 0;
  116. }
  117. mask_after = e->fields.mask;
  118. if (mask_before != mask_after)
  119. kvm_fire_mask_notifiers(ioapic->kvm, index, mask_after);
  120. if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
  121. && ioapic->irr & (1 << index))
  122. ioapic_service(ioapic, index);
  123. break;
  124. }
  125. }
  126. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  127. {
  128. union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
  129. struct kvm_lapic_irq irqe;
  130. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  131. "vector=%x trig_mode=%x\n",
  132. entry->fields.dest, entry->fields.dest_mode,
  133. entry->fields.delivery_mode, entry->fields.vector,
  134. entry->fields.trig_mode);
  135. irqe.dest_id = entry->fields.dest_id;
  136. irqe.vector = entry->fields.vector;
  137. irqe.dest_mode = entry->fields.dest_mode;
  138. irqe.trig_mode = entry->fields.trig_mode;
  139. irqe.delivery_mode = entry->fields.delivery_mode << 8;
  140. irqe.level = 1;
  141. irqe.shorthand = 0;
  142. #ifdef CONFIG_X86
  143. /* Always delivery PIT interrupt to vcpu 0 */
  144. if (irq == 0) {
  145. irqe.dest_mode = 0; /* Physical mode. */
  146. /* need to read apic_id from apic regiest since
  147. * it can be rewritten */
  148. irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id;
  149. }
  150. #endif
  151. return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
  152. }
  153. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  154. {
  155. u32 old_irr = ioapic->irr;
  156. u32 mask = 1 << irq;
  157. union kvm_ioapic_redirect_entry entry;
  158. int ret = 1;
  159. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  160. entry = ioapic->redirtbl[irq];
  161. level ^= entry.fields.polarity;
  162. if (!level)
  163. ioapic->irr &= ~mask;
  164. else {
  165. int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  166. ioapic->irr |= mask;
  167. if ((edge && old_irr != ioapic->irr) ||
  168. (!edge && !entry.fields.remote_irr))
  169. ret = ioapic_service(ioapic, irq);
  170. else
  171. ret = 0; /* report coalesced interrupt */
  172. }
  173. trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
  174. }
  175. return ret;
  176. }
  177. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int pin,
  178. int trigger_mode)
  179. {
  180. union kvm_ioapic_redirect_entry *ent;
  181. ent = &ioapic->redirtbl[pin];
  182. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, pin);
  183. if (trigger_mode == IOAPIC_LEVEL_TRIG) {
  184. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  185. ent->fields.remote_irr = 0;
  186. if (!ent->fields.mask && (ioapic->irr & (1 << pin)))
  187. ioapic_service(ioapic, pin);
  188. }
  189. }
  190. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
  191. {
  192. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  193. int i;
  194. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  195. if (ioapic->redirtbl[i].fields.vector == vector)
  196. __kvm_ioapic_update_eoi(ioapic, i, trigger_mode);
  197. }
  198. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  199. {
  200. return container_of(dev, struct kvm_ioapic, dev);
  201. }
  202. static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
  203. {
  204. return ((addr >= ioapic->base_address &&
  205. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  206. }
  207. static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  208. void *val)
  209. {
  210. struct kvm_ioapic *ioapic = to_ioapic(this);
  211. u32 result;
  212. if (!ioapic_in_range(ioapic, addr))
  213. return -EOPNOTSUPP;
  214. ioapic_debug("addr %lx\n", (unsigned long)addr);
  215. ASSERT(!(addr & 0xf)); /* check alignment */
  216. mutex_lock(&ioapic->kvm->irq_lock);
  217. addr &= 0xff;
  218. switch (addr) {
  219. case IOAPIC_REG_SELECT:
  220. result = ioapic->ioregsel;
  221. break;
  222. case IOAPIC_REG_WINDOW:
  223. result = ioapic_read_indirect(ioapic, addr, len);
  224. break;
  225. default:
  226. result = 0;
  227. break;
  228. }
  229. switch (len) {
  230. case 8:
  231. *(u64 *) val = result;
  232. break;
  233. case 1:
  234. case 2:
  235. case 4:
  236. memcpy(val, (char *)&result, len);
  237. break;
  238. default:
  239. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  240. }
  241. mutex_unlock(&ioapic->kvm->irq_lock);
  242. return 0;
  243. }
  244. static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  245. const void *val)
  246. {
  247. struct kvm_ioapic *ioapic = to_ioapic(this);
  248. u32 data;
  249. if (!ioapic_in_range(ioapic, addr))
  250. return -EOPNOTSUPP;
  251. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  252. (void*)addr, len, val);
  253. ASSERT(!(addr & 0xf)); /* check alignment */
  254. mutex_lock(&ioapic->kvm->irq_lock);
  255. if (len == 4 || len == 8)
  256. data = *(u32 *) val;
  257. else {
  258. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  259. goto unlock;
  260. }
  261. addr &= 0xff;
  262. switch (addr) {
  263. case IOAPIC_REG_SELECT:
  264. ioapic->ioregsel = data;
  265. break;
  266. case IOAPIC_REG_WINDOW:
  267. ioapic_write_indirect(ioapic, data);
  268. break;
  269. #ifdef CONFIG_IA64
  270. case IOAPIC_REG_EOI:
  271. kvm_ioapic_update_eoi(ioapic->kvm, data, IOAPIC_LEVEL_TRIG);
  272. break;
  273. #endif
  274. default:
  275. break;
  276. }
  277. unlock:
  278. mutex_unlock(&ioapic->kvm->irq_lock);
  279. return 0;
  280. }
  281. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  282. {
  283. int i;
  284. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  285. ioapic->redirtbl[i].fields.mask = 1;
  286. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  287. ioapic->ioregsel = 0;
  288. ioapic->irr = 0;
  289. ioapic->id = 0;
  290. }
  291. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  292. .read = ioapic_mmio_read,
  293. .write = ioapic_mmio_write,
  294. };
  295. int kvm_ioapic_init(struct kvm *kvm)
  296. {
  297. struct kvm_ioapic *ioapic;
  298. int ret;
  299. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  300. if (!ioapic)
  301. return -ENOMEM;
  302. kvm->arch.vioapic = ioapic;
  303. kvm_ioapic_reset(ioapic);
  304. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  305. ioapic->kvm = kvm;
  306. ret = kvm_io_bus_register_dev(kvm, &kvm->mmio_bus, &ioapic->dev);
  307. if (ret < 0)
  308. kfree(ioapic);
  309. return ret;
  310. }