ioapic.c 11 KB

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