ioapic.c 10 KB

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