ioapic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. int ioapic_deliver_entry(struct kvm *kvm, union kvm_ioapic_redirect_entry *e)
  125. {
  126. DECLARE_BITMAP(deliver_bitmask, KVM_MAX_VCPUS);
  127. int i, r = -1;
  128. kvm_get_intr_delivery_bitmask(kvm, e, deliver_bitmask);
  129. if (find_first_bit(deliver_bitmask, KVM_MAX_VCPUS) >= KVM_MAX_VCPUS) {
  130. ioapic_debug("no target on destination\n");
  131. return r;
  132. }
  133. while ((i = find_first_bit(deliver_bitmask, KVM_MAX_VCPUS))
  134. < KVM_MAX_VCPUS) {
  135. struct kvm_vcpu *vcpu = kvm->vcpus[i];
  136. __clear_bit(i, deliver_bitmask);
  137. if (vcpu) {
  138. if (r < 0)
  139. r = 0;
  140. r += kvm_apic_set_irq(vcpu, e->fields.vector,
  141. e->fields.delivery_mode,
  142. e->fields.trig_mode);
  143. } else
  144. ioapic_debug("null destination vcpu: "
  145. "mask=%x vector=%x delivery_mode=%x\n",
  146. e->fields.deliver_bitmask,
  147. e->fields.vector, e->fields.delivery_mode);
  148. }
  149. return r;
  150. }
  151. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  152. {
  153. union kvm_ioapic_redirect_entry entry = ioapic->redirtbl[irq];
  154. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  155. "vector=%x trig_mode=%x\n",
  156. entry.fields.dest, entry.fields.dest_mode,
  157. entry.fields.delivery_mode, entry.fields.vector,
  158. entry.fields.trig_mode);
  159. #ifdef CONFIG_X86
  160. /* Always delivery PIT interrupt to vcpu 0 */
  161. if (irq == 0) {
  162. entry.fields.dest_mode = 0; /* Physical mode. */
  163. entry.fields.dest_id = ioapic->kvm->vcpus[0]->vcpu_id;
  164. }
  165. #endif
  166. return ioapic_deliver_entry(ioapic->kvm, &entry);
  167. }
  168. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  169. {
  170. u32 old_irr = ioapic->irr;
  171. u32 mask = 1 << irq;
  172. union kvm_ioapic_redirect_entry entry;
  173. int ret = 1;
  174. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  175. entry = ioapic->redirtbl[irq];
  176. level ^= entry.fields.polarity;
  177. if (!level)
  178. ioapic->irr &= ~mask;
  179. else {
  180. ioapic->irr |= mask;
  181. if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
  182. || !entry.fields.remote_irr)
  183. ret = ioapic_service(ioapic, irq);
  184. }
  185. }
  186. return ret;
  187. }
  188. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int pin,
  189. int trigger_mode)
  190. {
  191. union kvm_ioapic_redirect_entry *ent;
  192. ent = &ioapic->redirtbl[pin];
  193. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, pin);
  194. if (trigger_mode == IOAPIC_LEVEL_TRIG) {
  195. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  196. ent->fields.remote_irr = 0;
  197. if (!ent->fields.mask && (ioapic->irr & (1 << pin)))
  198. ioapic_service(ioapic, pin);
  199. }
  200. }
  201. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
  202. {
  203. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  204. int i;
  205. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  206. if (ioapic->redirtbl[i].fields.vector == vector)
  207. __kvm_ioapic_update_eoi(ioapic, i, trigger_mode);
  208. }
  209. static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr,
  210. int len, int is_write)
  211. {
  212. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  213. return ((addr >= ioapic->base_address &&
  214. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  215. }
  216. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  217. void *val)
  218. {
  219. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  220. u32 result;
  221. ioapic_debug("addr %lx\n", (unsigned long)addr);
  222. ASSERT(!(addr & 0xf)); /* check alignment */
  223. addr &= 0xff;
  224. switch (addr) {
  225. case IOAPIC_REG_SELECT:
  226. result = ioapic->ioregsel;
  227. break;
  228. case IOAPIC_REG_WINDOW:
  229. result = ioapic_read_indirect(ioapic, addr, len);
  230. break;
  231. default:
  232. result = 0;
  233. break;
  234. }
  235. switch (len) {
  236. case 8:
  237. *(u64 *) val = result;
  238. break;
  239. case 1:
  240. case 2:
  241. case 4:
  242. memcpy(val, (char *)&result, len);
  243. break;
  244. default:
  245. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  246. }
  247. }
  248. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  249. const void *val)
  250. {
  251. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  252. u32 data;
  253. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  254. (void*)addr, len, val);
  255. ASSERT(!(addr & 0xf)); /* check alignment */
  256. if (len == 4 || len == 8)
  257. data = *(u32 *) val;
  258. else {
  259. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  260. return;
  261. }
  262. addr &= 0xff;
  263. switch (addr) {
  264. case IOAPIC_REG_SELECT:
  265. ioapic->ioregsel = data;
  266. break;
  267. case IOAPIC_REG_WINDOW:
  268. ioapic_write_indirect(ioapic, data);
  269. break;
  270. #ifdef CONFIG_IA64
  271. case IOAPIC_REG_EOI:
  272. kvm_ioapic_update_eoi(ioapic->kvm, data, IOAPIC_LEVEL_TRIG);
  273. break;
  274. #endif
  275. default:
  276. break;
  277. }
  278. }
  279. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  280. {
  281. int i;
  282. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  283. ioapic->redirtbl[i].fields.mask = 1;
  284. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  285. ioapic->ioregsel = 0;
  286. ioapic->irr = 0;
  287. ioapic->id = 0;
  288. }
  289. int kvm_ioapic_init(struct kvm *kvm)
  290. {
  291. struct kvm_ioapic *ioapic;
  292. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  293. if (!ioapic)
  294. return -ENOMEM;
  295. kvm->arch.vioapic = ioapic;
  296. kvm_ioapic_reset(ioapic);
  297. ioapic->dev.read = ioapic_mmio_read;
  298. ioapic->dev.write = ioapic_mmio_write;
  299. ioapic->dev.in_range = ioapic_in_range;
  300. ioapic->dev.private = ioapic;
  301. ioapic->kvm = kvm;
  302. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  303. return 0;
  304. }