ioapic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 void ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
  76. {
  77. union ioapic_redir_entry *pent;
  78. pent = &ioapic->redirtbl[idx];
  79. if (!pent->fields.mask) {
  80. int injected = ioapic_deliver(ioapic, idx);
  81. if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  82. pent->fields.remote_irr = 1;
  83. }
  84. if (!pent->fields.trig_mode)
  85. ioapic->irr &= ~(1 << idx);
  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->irr & (1 << index))
  118. ioapic_service(ioapic, index);
  119. break;
  120. }
  121. }
  122. static int ioapic_inj_irq(struct kvm_ioapic *ioapic,
  123. struct kvm_vcpu *vcpu,
  124. u8 vector, u8 trig_mode, u8 delivery_mode)
  125. {
  126. ioapic_debug("irq %d trig %d deliv %d\n", vector, trig_mode,
  127. delivery_mode);
  128. ASSERT((delivery_mode == IOAPIC_FIXED) ||
  129. (delivery_mode == IOAPIC_LOWEST_PRIORITY));
  130. return kvm_apic_set_irq(vcpu, vector, trig_mode);
  131. }
  132. static void ioapic_inj_nmi(struct kvm_vcpu *vcpu)
  133. {
  134. kvm_inject_nmi(vcpu);
  135. kvm_vcpu_kick(vcpu);
  136. }
  137. u32 kvm_ioapic_get_delivery_bitmask(struct kvm_ioapic *ioapic, u8 dest,
  138. u8 dest_mode)
  139. {
  140. u32 mask = 0;
  141. int i;
  142. struct kvm *kvm = ioapic->kvm;
  143. struct kvm_vcpu *vcpu;
  144. ioapic_debug("dest %d dest_mode %d\n", dest, dest_mode);
  145. if (dest_mode == 0) { /* Physical mode. */
  146. if (dest == 0xFF) { /* Broadcast. */
  147. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  148. if (kvm->vcpus[i] && kvm->vcpus[i]->arch.apic)
  149. mask |= 1 << i;
  150. return mask;
  151. }
  152. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  153. vcpu = kvm->vcpus[i];
  154. if (!vcpu)
  155. continue;
  156. if (kvm_apic_match_physical_addr(vcpu->arch.apic, dest)) {
  157. if (vcpu->arch.apic)
  158. mask = 1 << i;
  159. break;
  160. }
  161. }
  162. } else if (dest != 0) /* Logical mode, MDA non-zero. */
  163. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  164. vcpu = kvm->vcpus[i];
  165. if (!vcpu)
  166. continue;
  167. if (vcpu->arch.apic &&
  168. kvm_apic_match_logical_addr(vcpu->arch.apic, dest))
  169. mask |= 1 << vcpu->vcpu_id;
  170. }
  171. ioapic_debug("mask %x\n", mask);
  172. return mask;
  173. }
  174. static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  175. {
  176. u8 dest = ioapic->redirtbl[irq].fields.dest_id;
  177. u8 dest_mode = ioapic->redirtbl[irq].fields.dest_mode;
  178. u8 delivery_mode = ioapic->redirtbl[irq].fields.delivery_mode;
  179. u8 vector = ioapic->redirtbl[irq].fields.vector;
  180. u8 trig_mode = ioapic->redirtbl[irq].fields.trig_mode;
  181. u32 deliver_bitmask;
  182. struct kvm_vcpu *vcpu;
  183. int vcpu_id, r = 0;
  184. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  185. "vector=%x trig_mode=%x\n",
  186. dest, dest_mode, delivery_mode, vector, trig_mode);
  187. deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic, dest,
  188. dest_mode);
  189. if (!deliver_bitmask) {
  190. ioapic_debug("no target on destination\n");
  191. return 0;
  192. }
  193. switch (delivery_mode) {
  194. case IOAPIC_LOWEST_PRIORITY:
  195. vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
  196. deliver_bitmask);
  197. #ifdef CONFIG_X86
  198. if (irq == 0)
  199. vcpu = ioapic->kvm->vcpus[0];
  200. #endif
  201. if (vcpu != NULL)
  202. r = ioapic_inj_irq(ioapic, vcpu, vector,
  203. trig_mode, delivery_mode);
  204. else
  205. ioapic_debug("null lowest prio vcpu: "
  206. "mask=%x vector=%x delivery_mode=%x\n",
  207. deliver_bitmask, vector, IOAPIC_LOWEST_PRIORITY);
  208. break;
  209. case IOAPIC_FIXED:
  210. #ifdef CONFIG_X86
  211. if (irq == 0)
  212. deliver_bitmask = 1;
  213. #endif
  214. for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  215. if (!(deliver_bitmask & (1 << vcpu_id)))
  216. continue;
  217. deliver_bitmask &= ~(1 << vcpu_id);
  218. vcpu = ioapic->kvm->vcpus[vcpu_id];
  219. if (vcpu) {
  220. r = ioapic_inj_irq(ioapic, vcpu, vector,
  221. trig_mode, delivery_mode);
  222. }
  223. }
  224. break;
  225. case IOAPIC_NMI:
  226. for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  227. if (!(deliver_bitmask & (1 << vcpu_id)))
  228. continue;
  229. deliver_bitmask &= ~(1 << vcpu_id);
  230. vcpu = ioapic->kvm->vcpus[vcpu_id];
  231. if (vcpu)
  232. ioapic_inj_nmi(vcpu);
  233. else
  234. ioapic_debug("NMI to vcpu %d failed\n",
  235. vcpu->vcpu_id);
  236. }
  237. break;
  238. default:
  239. printk(KERN_WARNING "Unsupported delivery mode %d\n",
  240. delivery_mode);
  241. break;
  242. }
  243. return r;
  244. }
  245. void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  246. {
  247. u32 old_irr = ioapic->irr;
  248. u32 mask = 1 << irq;
  249. union ioapic_redir_entry entry;
  250. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  251. entry = ioapic->redirtbl[irq];
  252. level ^= entry.fields.polarity;
  253. if (!level)
  254. ioapic->irr &= ~mask;
  255. else {
  256. ioapic->irr |= mask;
  257. if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
  258. || !entry.fields.remote_irr)
  259. ioapic_service(ioapic, irq);
  260. }
  261. }
  262. }
  263. static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int gsi,
  264. int trigger_mode)
  265. {
  266. union ioapic_redir_entry *ent;
  267. ent = &ioapic->redirtbl[gsi];
  268. kvm_notify_acked_irq(ioapic->kvm, gsi);
  269. if (trigger_mode == IOAPIC_LEVEL_TRIG) {
  270. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  271. ent->fields.remote_irr = 0;
  272. if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
  273. ioapic_service(ioapic, gsi);
  274. }
  275. }
  276. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
  277. {
  278. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  279. int i;
  280. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  281. if (ioapic->redirtbl[i].fields.vector == vector)
  282. __kvm_ioapic_update_eoi(ioapic, i, trigger_mode);
  283. }
  284. static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr,
  285. int len, int is_write)
  286. {
  287. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  288. return ((addr >= ioapic->base_address &&
  289. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  290. }
  291. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  292. void *val)
  293. {
  294. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  295. u32 result;
  296. ioapic_debug("addr %lx\n", (unsigned long)addr);
  297. ASSERT(!(addr & 0xf)); /* check alignment */
  298. addr &= 0xff;
  299. switch (addr) {
  300. case IOAPIC_REG_SELECT:
  301. result = ioapic->ioregsel;
  302. break;
  303. case IOAPIC_REG_WINDOW:
  304. result = ioapic_read_indirect(ioapic, addr, len);
  305. break;
  306. default:
  307. result = 0;
  308. break;
  309. }
  310. switch (len) {
  311. case 8:
  312. *(u64 *) val = result;
  313. break;
  314. case 1:
  315. case 2:
  316. case 4:
  317. memcpy(val, (char *)&result, len);
  318. break;
  319. default:
  320. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  321. }
  322. }
  323. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  324. const void *val)
  325. {
  326. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  327. u32 data;
  328. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  329. (void*)addr, len, val);
  330. ASSERT(!(addr & 0xf)); /* check alignment */
  331. if (len == 4 || len == 8)
  332. data = *(u32 *) val;
  333. else {
  334. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  335. return;
  336. }
  337. addr &= 0xff;
  338. switch (addr) {
  339. case IOAPIC_REG_SELECT:
  340. ioapic->ioregsel = data;
  341. break;
  342. case IOAPIC_REG_WINDOW:
  343. ioapic_write_indirect(ioapic, data);
  344. break;
  345. #ifdef CONFIG_IA64
  346. case IOAPIC_REG_EOI:
  347. kvm_ioapic_update_eoi(ioapic->kvm, data, IOAPIC_LEVEL_TRIG);
  348. break;
  349. #endif
  350. default:
  351. break;
  352. }
  353. }
  354. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  355. {
  356. int i;
  357. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  358. ioapic->redirtbl[i].fields.mask = 1;
  359. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  360. ioapic->ioregsel = 0;
  361. ioapic->irr = 0;
  362. ioapic->id = 0;
  363. }
  364. int kvm_ioapic_init(struct kvm *kvm)
  365. {
  366. struct kvm_ioapic *ioapic;
  367. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  368. if (!ioapic)
  369. return -ENOMEM;
  370. kvm->arch.vioapic = ioapic;
  371. kvm_ioapic_reset(ioapic);
  372. ioapic->dev.read = ioapic_mmio_read;
  373. ioapic->dev.write = ioapic_mmio_write;
  374. ioapic->dev.in_range = ioapic_in_range;
  375. ioapic->dev.private = ioapic;
  376. ioapic->kvm = kvm;
  377. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  378. return 0;
  379. }