ioapic.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. if (!pent->fields.trig_mode)
  86. ioapic->irr &= ~(1 << idx);
  87. return injected;
  88. }
  89. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  90. {
  91. unsigned index;
  92. bool mask_before, mask_after;
  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. mask_before = ioapic->redirtbl[index].fields.mask;
  108. if (ioapic->ioregsel & 1) {
  109. ioapic->redirtbl[index].bits &= 0xffffffff;
  110. ioapic->redirtbl[index].bits |= (u64) val << 32;
  111. } else {
  112. ioapic->redirtbl[index].bits &= ~0xffffffffULL;
  113. ioapic->redirtbl[index].bits |= (u32) val;
  114. ioapic->redirtbl[index].fields.remote_irr = 0;
  115. }
  116. mask_after = ioapic->redirtbl[index].fields.mask;
  117. if (mask_before != mask_after)
  118. kvm_fire_mask_notifiers(ioapic->kvm, index, mask_after);
  119. if (ioapic->irr & (1 << index))
  120. ioapic_service(ioapic, index);
  121. break;
  122. }
  123. }
  124. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  125. {
  126. union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
  127. struct kvm_lapic_irq irqe;
  128. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  129. "vector=%x trig_mode=%x\n",
  130. entry->fields.dest, entry->fields.dest_mode,
  131. entry->fields.delivery_mode, entry->fields.vector,
  132. entry->fields.trig_mode);
  133. irqe.dest_id = entry->fields.dest_id;
  134. irqe.vector = entry->fields.vector;
  135. irqe.dest_mode = entry->fields.dest_mode;
  136. irqe.trig_mode = entry->fields.trig_mode;
  137. irqe.delivery_mode = entry->fields.delivery_mode << 8;
  138. irqe.level = 1;
  139. irqe.shorthand = 0;
  140. #ifdef CONFIG_X86
  141. /* Always delivery PIT interrupt to vcpu 0 */
  142. if (irq == 0) {
  143. irqe.dest_mode = 0; /* Physical mode. */
  144. irqe.dest_id = ioapic->kvm->vcpus[0]->vcpu_id;
  145. }
  146. #endif
  147. return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
  148. }
  149. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  150. {
  151. u32 old_irr = ioapic->irr;
  152. u32 mask = 1 << irq;
  153. union kvm_ioapic_redirect_entry entry;
  154. int ret = 1;
  155. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  156. entry = ioapic->redirtbl[irq];
  157. level ^= entry.fields.polarity;
  158. if (!level)
  159. ioapic->irr &= ~mask;
  160. else {
  161. ioapic->irr |= mask;
  162. if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
  163. || !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 int ioapic_in_range(struct kvm_io_device *this, gpa_t addr,
  191. int len, int is_write)
  192. {
  193. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  194. return ((addr >= ioapic->base_address &&
  195. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  196. }
  197. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  198. void *val)
  199. {
  200. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  201. u32 result;
  202. ioapic_debug("addr %lx\n", (unsigned long)addr);
  203. ASSERT(!(addr & 0xf)); /* check alignment */
  204. addr &= 0xff;
  205. switch (addr) {
  206. case IOAPIC_REG_SELECT:
  207. result = ioapic->ioregsel;
  208. break;
  209. case IOAPIC_REG_WINDOW:
  210. result = ioapic_read_indirect(ioapic, addr, len);
  211. break;
  212. default:
  213. result = 0;
  214. break;
  215. }
  216. switch (len) {
  217. case 8:
  218. *(u64 *) val = result;
  219. break;
  220. case 1:
  221. case 2:
  222. case 4:
  223. memcpy(val, (char *)&result, len);
  224. break;
  225. default:
  226. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  227. }
  228. }
  229. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  230. const void *val)
  231. {
  232. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  233. u32 data;
  234. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  235. (void*)addr, len, val);
  236. ASSERT(!(addr & 0xf)); /* check alignment */
  237. if (len == 4 || len == 8)
  238. data = *(u32 *) val;
  239. else {
  240. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  241. return;
  242. }
  243. addr &= 0xff;
  244. switch (addr) {
  245. case IOAPIC_REG_SELECT:
  246. ioapic->ioregsel = data;
  247. break;
  248. case IOAPIC_REG_WINDOW:
  249. ioapic_write_indirect(ioapic, data);
  250. break;
  251. #ifdef CONFIG_IA64
  252. case IOAPIC_REG_EOI:
  253. kvm_ioapic_update_eoi(ioapic->kvm, data, IOAPIC_LEVEL_TRIG);
  254. break;
  255. #endif
  256. default:
  257. break;
  258. }
  259. }
  260. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  261. {
  262. int i;
  263. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  264. ioapic->redirtbl[i].fields.mask = 1;
  265. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  266. ioapic->ioregsel = 0;
  267. ioapic->irr = 0;
  268. ioapic->id = 0;
  269. }
  270. int kvm_ioapic_init(struct kvm *kvm)
  271. {
  272. struct kvm_ioapic *ioapic;
  273. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  274. if (!ioapic)
  275. return -ENOMEM;
  276. kvm->arch.vioapic = ioapic;
  277. kvm_ioapic_reset(ioapic);
  278. ioapic->dev.read = ioapic_mmio_read;
  279. ioapic->dev.write = ioapic_mmio_write;
  280. ioapic->dev.in_range = ioapic_in_range;
  281. ioapic->dev.private = ioapic;
  282. ioapic->kvm = kvm;
  283. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  284. return 0;
  285. }