ioapic.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #if 0
  41. #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
  42. #else
  43. #define ioapic_debug(fmt, arg...)
  44. #endif
  45. static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
  46. static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
  47. unsigned long addr,
  48. unsigned long length)
  49. {
  50. unsigned long result = 0;
  51. switch (ioapic->ioregsel) {
  52. case IOAPIC_REG_VERSION:
  53. result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
  54. | (IOAPIC_VERSION_ID & 0xff));
  55. break;
  56. case IOAPIC_REG_APIC_ID:
  57. case IOAPIC_REG_ARB_ID:
  58. result = ((ioapic->id & 0xf) << 24);
  59. break;
  60. default:
  61. {
  62. u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
  63. u64 redir_content;
  64. ASSERT(redir_index < IOAPIC_NUM_PINS);
  65. redir_content = ioapic->redirtbl[redir_index].bits;
  66. result = (ioapic->ioregsel & 0x1) ?
  67. (redir_content >> 32) & 0xffffffff :
  68. redir_content & 0xffffffff;
  69. break;
  70. }
  71. }
  72. return result;
  73. }
  74. static void ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
  75. {
  76. union ioapic_redir_entry *pent;
  77. pent = &ioapic->redirtbl[idx];
  78. if (!pent->fields.mask) {
  79. int injected = ioapic_deliver(ioapic, idx);
  80. if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  81. pent->fields.remote_irr = 1;
  82. }
  83. if (!pent->fields.trig_mode)
  84. ioapic->irr &= ~(1 << idx);
  85. }
  86. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  87. {
  88. unsigned index;
  89. switch (ioapic->ioregsel) {
  90. case IOAPIC_REG_VERSION:
  91. /* Writes are ignored. */
  92. break;
  93. case IOAPIC_REG_APIC_ID:
  94. ioapic->id = (val >> 24) & 0xf;
  95. break;
  96. case IOAPIC_REG_ARB_ID:
  97. break;
  98. default:
  99. index = (ioapic->ioregsel - 0x10) >> 1;
  100. ioapic_debug("change redir index %x val %x\n", index, val);
  101. if (index >= IOAPIC_NUM_PINS)
  102. return;
  103. if (ioapic->ioregsel & 1) {
  104. ioapic->redirtbl[index].bits &= 0xffffffff;
  105. ioapic->redirtbl[index].bits |= (u64) val << 32;
  106. } else {
  107. ioapic->redirtbl[index].bits &= ~0xffffffffULL;
  108. ioapic->redirtbl[index].bits |= (u32) val;
  109. ioapic->redirtbl[index].fields.remote_irr = 0;
  110. }
  111. if (ioapic->irr & (1 << index))
  112. ioapic_service(ioapic, index);
  113. break;
  114. }
  115. }
  116. static int ioapic_inj_irq(struct kvm_ioapic *ioapic,
  117. struct kvm_vcpu *vcpu,
  118. u8 vector, u8 trig_mode, u8 delivery_mode)
  119. {
  120. ioapic_debug("irq %d trig %d deliv %d\n", vector, trig_mode,
  121. delivery_mode);
  122. ASSERT((delivery_mode == IOAPIC_FIXED) ||
  123. (delivery_mode == IOAPIC_LOWEST_PRIORITY));
  124. return kvm_apic_set_irq(vcpu, vector, trig_mode);
  125. }
  126. static u32 ioapic_get_delivery_bitmask(struct kvm_ioapic *ioapic, u8 dest,
  127. u8 dest_mode)
  128. {
  129. u32 mask = 0;
  130. int i;
  131. struct kvm *kvm = ioapic->kvm;
  132. struct kvm_vcpu *vcpu;
  133. ioapic_debug("dest %d dest_mode %d\n", dest, dest_mode);
  134. if (dest_mode == 0) { /* Physical mode. */
  135. if (dest == 0xFF) { /* Broadcast. */
  136. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  137. if (kvm->vcpus[i] && kvm->vcpus[i]->arch.apic)
  138. mask |= 1 << i;
  139. return mask;
  140. }
  141. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  142. vcpu = kvm->vcpus[i];
  143. if (!vcpu)
  144. continue;
  145. if (kvm_apic_match_physical_addr(vcpu->arch.apic, dest)) {
  146. if (vcpu->arch.apic)
  147. mask = 1 << i;
  148. break;
  149. }
  150. }
  151. } else if (dest != 0) /* Logical mode, MDA non-zero. */
  152. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  153. vcpu = kvm->vcpus[i];
  154. if (!vcpu)
  155. continue;
  156. if (vcpu->arch.apic &&
  157. kvm_apic_match_logical_addr(vcpu->arch.apic, dest))
  158. mask |= 1 << vcpu->vcpu_id;
  159. }
  160. ioapic_debug("mask %x\n", mask);
  161. return mask;
  162. }
  163. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  164. {
  165. u8 dest = ioapic->redirtbl[irq].fields.dest_id;
  166. u8 dest_mode = ioapic->redirtbl[irq].fields.dest_mode;
  167. u8 delivery_mode = ioapic->redirtbl[irq].fields.delivery_mode;
  168. u8 vector = ioapic->redirtbl[irq].fields.vector;
  169. u8 trig_mode = ioapic->redirtbl[irq].fields.trig_mode;
  170. u32 deliver_bitmask;
  171. struct kvm_vcpu *vcpu;
  172. int vcpu_id, r = 0;
  173. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  174. "vector=%x trig_mode=%x\n",
  175. dest, dest_mode, delivery_mode, vector, trig_mode);
  176. deliver_bitmask = ioapic_get_delivery_bitmask(ioapic, dest, dest_mode);
  177. if (!deliver_bitmask) {
  178. ioapic_debug("no target on destination\n");
  179. return 0;
  180. }
  181. switch (delivery_mode) {
  182. case IOAPIC_LOWEST_PRIORITY:
  183. vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
  184. deliver_bitmask);
  185. #ifdef CONFIG_X86
  186. if (irq == 0)
  187. vcpu = ioapic->kvm->vcpus[0];
  188. #endif
  189. if (vcpu != NULL)
  190. r = ioapic_inj_irq(ioapic, vcpu, vector,
  191. trig_mode, delivery_mode);
  192. else
  193. ioapic_debug("null lowest prio vcpu: "
  194. "mask=%x vector=%x delivery_mode=%x\n",
  195. deliver_bitmask, vector, IOAPIC_LOWEST_PRIORITY);
  196. break;
  197. case IOAPIC_FIXED:
  198. #ifdef CONFIG_X86
  199. if (irq == 0)
  200. deliver_bitmask = 1;
  201. #endif
  202. for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  203. if (!(deliver_bitmask & (1 << vcpu_id)))
  204. continue;
  205. deliver_bitmask &= ~(1 << vcpu_id);
  206. vcpu = ioapic->kvm->vcpus[vcpu_id];
  207. if (vcpu) {
  208. r = ioapic_inj_irq(ioapic, vcpu, vector,
  209. trig_mode, delivery_mode);
  210. }
  211. }
  212. break;
  213. /* TODO: NMI */
  214. default:
  215. printk(KERN_WARNING "Unsupported delivery mode %d\n",
  216. delivery_mode);
  217. break;
  218. }
  219. return r;
  220. }
  221. void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  222. {
  223. u32 old_irr = ioapic->irr;
  224. u32 mask = 1 << irq;
  225. union ioapic_redir_entry entry;
  226. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  227. entry = ioapic->redirtbl[irq];
  228. level ^= entry.fields.polarity;
  229. if (!level)
  230. ioapic->irr &= ~mask;
  231. else {
  232. ioapic->irr |= mask;
  233. if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
  234. || !entry.fields.remote_irr)
  235. ioapic_service(ioapic, irq);
  236. }
  237. }
  238. }
  239. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi)
  240. {
  241. union ioapic_redir_entry *ent;
  242. ent = &ioapic->redirtbl[gsi];
  243. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  244. ent->fields.remote_irr = 0;
  245. if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
  246. ioapic_service(ioapic, gsi);
  247. }
  248. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector)
  249. {
  250. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  251. int i;
  252. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  253. if (ioapic->redirtbl[i].fields.vector == vector)
  254. __kvm_ioapic_update_eoi(ioapic, i);
  255. }
  256. static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr)
  257. {
  258. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  259. return ((addr >= ioapic->base_address &&
  260. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  261. }
  262. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  263. void *val)
  264. {
  265. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  266. u32 result;
  267. ioapic_debug("addr %lx\n", (unsigned long)addr);
  268. ASSERT(!(addr & 0xf)); /* check alignment */
  269. addr &= 0xff;
  270. switch (addr) {
  271. case IOAPIC_REG_SELECT:
  272. result = ioapic->ioregsel;
  273. break;
  274. case IOAPIC_REG_WINDOW:
  275. result = ioapic_read_indirect(ioapic, addr, len);
  276. break;
  277. default:
  278. result = 0;
  279. break;
  280. }
  281. switch (len) {
  282. case 8:
  283. *(u64 *) val = result;
  284. break;
  285. case 1:
  286. case 2:
  287. case 4:
  288. memcpy(val, (char *)&result, len);
  289. break;
  290. default:
  291. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  292. }
  293. }
  294. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  295. const void *val)
  296. {
  297. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  298. u32 data;
  299. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  300. (void*)addr, len, val);
  301. ASSERT(!(addr & 0xf)); /* check alignment */
  302. if (len == 4 || len == 8)
  303. data = *(u32 *) val;
  304. else {
  305. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  306. return;
  307. }
  308. addr &= 0xff;
  309. switch (addr) {
  310. case IOAPIC_REG_SELECT:
  311. ioapic->ioregsel = data;
  312. break;
  313. case IOAPIC_REG_WINDOW:
  314. ioapic_write_indirect(ioapic, data);
  315. break;
  316. #ifdef CONFIG_IA64
  317. case IOAPIC_REG_EOI:
  318. kvm_ioapic_update_eoi(ioapic->kvm, data);
  319. break;
  320. #endif
  321. default:
  322. break;
  323. }
  324. }
  325. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  326. {
  327. int i;
  328. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  329. ioapic->redirtbl[i].fields.mask = 1;
  330. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  331. ioapic->ioregsel = 0;
  332. ioapic->irr = 0;
  333. ioapic->id = 0;
  334. }
  335. int kvm_ioapic_init(struct kvm *kvm)
  336. {
  337. struct kvm_ioapic *ioapic;
  338. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  339. if (!ioapic)
  340. return -ENOMEM;
  341. kvm->arch.vioapic = ioapic;
  342. kvm_ioapic_reset(ioapic);
  343. ioapic->dev.read = ioapic_mmio_read;
  344. ioapic->dev.write = ioapic_mmio_write;
  345. ioapic->dev.in_range = ioapic_in_range;
  346. ioapic->dev.private = ioapic;
  347. ioapic->kvm = kvm;
  348. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  349. return 0;
  350. }