ioapic.c 8.3 KB

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