ioapic.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /* need to read apic_id from apic regiest since
  144. * it can be rewritten */
  145. irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id;
  146. }
  147. #endif
  148. return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
  149. }
  150. int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  151. {
  152. u32 old_irr = ioapic->irr;
  153. u32 mask = 1 << irq;
  154. union kvm_ioapic_redirect_entry entry;
  155. int ret = 1;
  156. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  157. entry = ioapic->redirtbl[irq];
  158. level ^= entry.fields.polarity;
  159. if (!level)
  160. ioapic->irr &= ~mask;
  161. else {
  162. int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
  163. ioapic->irr |= mask;
  164. if ((edge && old_irr != ioapic->irr) ||
  165. (!edge && !entry.fields.remote_irr))
  166. ret = ioapic_service(ioapic, irq);
  167. }
  168. }
  169. return ret;
  170. }
  171. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int pin,
  172. int trigger_mode)
  173. {
  174. union kvm_ioapic_redirect_entry *ent;
  175. ent = &ioapic->redirtbl[pin];
  176. kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, pin);
  177. if (trigger_mode == IOAPIC_LEVEL_TRIG) {
  178. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  179. ent->fields.remote_irr = 0;
  180. if (!ent->fields.mask && (ioapic->irr & (1 << pin)))
  181. ioapic_service(ioapic, pin);
  182. }
  183. }
  184. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
  185. {
  186. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  187. int i;
  188. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  189. if (ioapic->redirtbl[i].fields.vector == vector)
  190. __kvm_ioapic_update_eoi(ioapic, i, trigger_mode);
  191. }
  192. static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
  193. {
  194. return container_of(dev, struct kvm_ioapic, dev);
  195. }
  196. static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr,
  197. int len, int is_write)
  198. {
  199. struct kvm_ioapic *ioapic = to_ioapic(this);
  200. return ((addr >= ioapic->base_address &&
  201. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  202. }
  203. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  204. void *val)
  205. {
  206. struct kvm_ioapic *ioapic = to_ioapic(this);
  207. u32 result;
  208. ioapic_debug("addr %lx\n", (unsigned long)addr);
  209. ASSERT(!(addr & 0xf)); /* check alignment */
  210. mutex_lock(&ioapic->kvm->irq_lock);
  211. addr &= 0xff;
  212. switch (addr) {
  213. case IOAPIC_REG_SELECT:
  214. result = ioapic->ioregsel;
  215. break;
  216. case IOAPIC_REG_WINDOW:
  217. result = ioapic_read_indirect(ioapic, addr, len);
  218. break;
  219. default:
  220. result = 0;
  221. break;
  222. }
  223. switch (len) {
  224. case 8:
  225. *(u64 *) val = result;
  226. break;
  227. case 1:
  228. case 2:
  229. case 4:
  230. memcpy(val, (char *)&result, len);
  231. break;
  232. default:
  233. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  234. }
  235. mutex_unlock(&ioapic->kvm->irq_lock);
  236. }
  237. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  238. const void *val)
  239. {
  240. struct kvm_ioapic *ioapic = to_ioapic(this);
  241. u32 data;
  242. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  243. (void*)addr, len, val);
  244. ASSERT(!(addr & 0xf)); /* check alignment */
  245. mutex_lock(&ioapic->kvm->irq_lock);
  246. if (len == 4 || len == 8)
  247. data = *(u32 *) val;
  248. else {
  249. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  250. return;
  251. }
  252. addr &= 0xff;
  253. switch (addr) {
  254. case IOAPIC_REG_SELECT:
  255. ioapic->ioregsel = data;
  256. break;
  257. case IOAPIC_REG_WINDOW:
  258. ioapic_write_indirect(ioapic, data);
  259. break;
  260. #ifdef CONFIG_IA64
  261. case IOAPIC_REG_EOI:
  262. kvm_ioapic_update_eoi(ioapic->kvm, data, IOAPIC_LEVEL_TRIG);
  263. break;
  264. #endif
  265. default:
  266. break;
  267. }
  268. mutex_unlock(&ioapic->kvm->irq_lock);
  269. }
  270. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  271. {
  272. int i;
  273. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  274. ioapic->redirtbl[i].fields.mask = 1;
  275. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  276. ioapic->ioregsel = 0;
  277. ioapic->irr = 0;
  278. ioapic->id = 0;
  279. }
  280. static const struct kvm_io_device_ops ioapic_mmio_ops = {
  281. .read = ioapic_mmio_read,
  282. .write = ioapic_mmio_write,
  283. .in_range = ioapic_in_range,
  284. };
  285. int kvm_ioapic_init(struct kvm *kvm)
  286. {
  287. struct kvm_ioapic *ioapic;
  288. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  289. if (!ioapic)
  290. return -ENOMEM;
  291. kvm->arch.vioapic = ioapic;
  292. kvm_ioapic_reset(ioapic);
  293. kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
  294. ioapic->kvm = kvm;
  295. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  296. return 0;
  297. }