ioapic.c 10 KB

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