ioapic.c 10 KB

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