ioapic.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "kvm.h"
  29. #include "x86.h"
  30. #include <linux/kvm.h>
  31. #include <linux/mm.h>
  32. #include <linux/highmem.h>
  33. #include <linux/smp.h>
  34. #include <linux/hrtimer.h>
  35. #include <linux/io.h>
  36. #include <asm/processor.h>
  37. #include <asm/msr.h>
  38. #include <asm/page.h>
  39. #include <asm/current.h>
  40. #include <asm/apicdef.h>
  41. #include <asm/io_apic.h>
  42. #include "irq.h"
  43. #if 0
  44. #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
  45. #else
  46. #define ioapic_debug(fmt, arg...)
  47. #endif
  48. static void ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
  49. static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
  50. unsigned long addr,
  51. unsigned long length)
  52. {
  53. unsigned long result = 0;
  54. switch (ioapic->ioregsel) {
  55. case IOAPIC_REG_VERSION:
  56. result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
  57. | (IOAPIC_VERSION_ID & 0xff));
  58. break;
  59. case IOAPIC_REG_APIC_ID:
  60. case IOAPIC_REG_ARB_ID:
  61. result = ((ioapic->id & 0xf) << 24);
  62. break;
  63. default:
  64. {
  65. u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
  66. u64 redir_content;
  67. ASSERT(redir_index < IOAPIC_NUM_PINS);
  68. redir_content = ioapic->redirtbl[redir_index].bits;
  69. result = (ioapic->ioregsel & 0x1) ?
  70. (redir_content >> 32) & 0xffffffff :
  71. redir_content & 0xffffffff;
  72. break;
  73. }
  74. }
  75. return result;
  76. }
  77. static void ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
  78. {
  79. union ioapic_redir_entry *pent;
  80. pent = &ioapic->redirtbl[idx];
  81. if (!pent->fields.mask) {
  82. ioapic_deliver(ioapic, idx);
  83. if (pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
  84. pent->fields.remote_irr = 1;
  85. }
  86. if (!pent->fields.trig_mode)
  87. ioapic->irr &= ~(1 << idx);
  88. }
  89. static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
  90. {
  91. unsigned index;
  92. switch (ioapic->ioregsel) {
  93. case IOAPIC_REG_VERSION:
  94. /* Writes are ignored. */
  95. break;
  96. case IOAPIC_REG_APIC_ID:
  97. ioapic->id = (val >> 24) & 0xf;
  98. break;
  99. case IOAPIC_REG_ARB_ID:
  100. break;
  101. default:
  102. index = (ioapic->ioregsel - 0x10) >> 1;
  103. ioapic_debug("change redir index %x val %x\n", index, val);
  104. if (index >= IOAPIC_NUM_PINS)
  105. return;
  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. if (ioapic->irr & (1 << index))
  115. ioapic_service(ioapic, index);
  116. break;
  117. }
  118. }
  119. static void ioapic_inj_irq(struct kvm_ioapic *ioapic,
  120. struct kvm_lapic *target,
  121. u8 vector, u8 trig_mode, u8 delivery_mode)
  122. {
  123. ioapic_debug("irq %d trig %d deliv %d\n", vector, trig_mode,
  124. delivery_mode);
  125. ASSERT((delivery_mode == dest_Fixed) ||
  126. (delivery_mode == dest_LowestPrio));
  127. kvm_apic_set_irq(target, vector, trig_mode);
  128. }
  129. static u32 ioapic_get_delivery_bitmask(struct kvm_ioapic *ioapic, u8 dest,
  130. u8 dest_mode)
  131. {
  132. u32 mask = 0;
  133. int i;
  134. struct kvm *kvm = ioapic->kvm;
  135. struct kvm_vcpu *vcpu;
  136. ioapic_debug("dest %d dest_mode %d\n", dest, dest_mode);
  137. if (dest_mode == 0) { /* Physical mode. */
  138. if (dest == 0xFF) { /* Broadcast. */
  139. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  140. if (kvm->vcpus[i] && kvm->vcpus[i]->apic)
  141. mask |= 1 << i;
  142. return mask;
  143. }
  144. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  145. vcpu = kvm->vcpus[i];
  146. if (!vcpu)
  147. continue;
  148. if (kvm_apic_match_physical_addr(vcpu->apic, dest)) {
  149. if (vcpu->apic)
  150. mask = 1 << i;
  151. break;
  152. }
  153. }
  154. } else if (dest != 0) /* Logical mode, MDA non-zero. */
  155. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  156. vcpu = kvm->vcpus[i];
  157. if (!vcpu)
  158. continue;
  159. if (vcpu->apic &&
  160. kvm_apic_match_logical_addr(vcpu->apic, dest))
  161. mask |= 1 << vcpu->vcpu_id;
  162. }
  163. ioapic_debug("mask %x\n", mask);
  164. return mask;
  165. }
  166. static void ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
  167. {
  168. u8 dest = ioapic->redirtbl[irq].fields.dest_id;
  169. u8 dest_mode = ioapic->redirtbl[irq].fields.dest_mode;
  170. u8 delivery_mode = ioapic->redirtbl[irq].fields.delivery_mode;
  171. u8 vector = ioapic->redirtbl[irq].fields.vector;
  172. u8 trig_mode = ioapic->redirtbl[irq].fields.trig_mode;
  173. u32 deliver_bitmask;
  174. struct kvm_lapic *target;
  175. struct kvm_vcpu *vcpu;
  176. int vcpu_id;
  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;
  184. }
  185. switch (delivery_mode) {
  186. case dest_LowestPrio:
  187. target =
  188. kvm_apic_round_robin(ioapic->kvm, vector, deliver_bitmask);
  189. if (target != NULL)
  190. ioapic_inj_irq(ioapic, target, vector,
  191. trig_mode, delivery_mode);
  192. else
  193. ioapic_debug("null round robin: "
  194. "mask=%x vector=%x delivery_mode=%x\n",
  195. deliver_bitmask, vector, dest_LowestPrio);
  196. break;
  197. case dest_Fixed:
  198. for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  199. if (!(deliver_bitmask & (1 << vcpu_id)))
  200. continue;
  201. deliver_bitmask &= ~(1 << vcpu_id);
  202. vcpu = ioapic->kvm->vcpus[vcpu_id];
  203. if (vcpu) {
  204. target = vcpu->apic;
  205. ioapic_inj_irq(ioapic, target, vector,
  206. trig_mode, delivery_mode);
  207. }
  208. }
  209. break;
  210. /* TODO: NMI */
  211. default:
  212. printk(KERN_WARNING "Unsupported delivery mode %d\n",
  213. delivery_mode);
  214. break;
  215. }
  216. }
  217. void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  218. {
  219. u32 old_irr = ioapic->irr;
  220. u32 mask = 1 << irq;
  221. union ioapic_redir_entry entry;
  222. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  223. entry = ioapic->redirtbl[irq];
  224. level ^= entry.fields.polarity;
  225. if (!level)
  226. ioapic->irr &= ~mask;
  227. else {
  228. ioapic->irr |= mask;
  229. if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
  230. || !entry.fields.remote_irr)
  231. ioapic_service(ioapic, irq);
  232. }
  233. }
  234. }
  235. static int get_eoi_gsi(struct kvm_ioapic *ioapic, int vector)
  236. {
  237. int i;
  238. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  239. if (ioapic->redirtbl[i].fields.vector == vector)
  240. return i;
  241. return -1;
  242. }
  243. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector)
  244. {
  245. struct kvm_ioapic *ioapic = kvm->vioapic;
  246. union ioapic_redir_entry *ent;
  247. int gsi;
  248. gsi = get_eoi_gsi(ioapic, vector);
  249. if (gsi == -1) {
  250. printk(KERN_WARNING "Can't find redir item for %d EOI\n",
  251. vector);
  252. return;
  253. }
  254. ent = &ioapic->redirtbl[gsi];
  255. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  256. ent->fields.remote_irr = 0;
  257. if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
  258. ioapic_deliver(ioapic, gsi);
  259. }
  260. static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr)
  261. {
  262. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  263. return ((addr >= ioapic->base_address &&
  264. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  265. }
  266. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  267. void *val)
  268. {
  269. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  270. u32 result;
  271. ioapic_debug("addr %lx\n", (unsigned long)addr);
  272. ASSERT(!(addr & 0xf)); /* check alignment */
  273. addr &= 0xff;
  274. switch (addr) {
  275. case IOAPIC_REG_SELECT:
  276. result = ioapic->ioregsel;
  277. break;
  278. case IOAPIC_REG_WINDOW:
  279. result = ioapic_read_indirect(ioapic, addr, len);
  280. break;
  281. default:
  282. result = 0;
  283. break;
  284. }
  285. switch (len) {
  286. case 8:
  287. *(u64 *) val = result;
  288. break;
  289. case 1:
  290. case 2:
  291. case 4:
  292. memcpy(val, (char *)&result, len);
  293. break;
  294. default:
  295. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  296. }
  297. }
  298. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  299. const void *val)
  300. {
  301. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  302. u32 data;
  303. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  304. (void*)addr, len, val);
  305. ASSERT(!(addr & 0xf)); /* check alignment */
  306. if (len == 4 || len == 8)
  307. data = *(u32 *) val;
  308. else {
  309. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  310. return;
  311. }
  312. addr &= 0xff;
  313. switch (addr) {
  314. case IOAPIC_REG_SELECT:
  315. ioapic->ioregsel = data;
  316. break;
  317. case IOAPIC_REG_WINDOW:
  318. ioapic_write_indirect(ioapic, data);
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  325. {
  326. int i;
  327. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  328. ioapic->redirtbl[i].fields.mask = 1;
  329. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  330. ioapic->ioregsel = 0;
  331. ioapic->irr = 0;
  332. ioapic->id = 0;
  333. }
  334. int kvm_ioapic_init(struct kvm *kvm)
  335. {
  336. struct kvm_ioapic *ioapic;
  337. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  338. if (!ioapic)
  339. return -ENOMEM;
  340. kvm->vioapic = ioapic;
  341. kvm_ioapic_reset(ioapic);
  342. ioapic->dev.read = ioapic_mmio_read;
  343. ioapic->dev.write = ioapic_mmio_write;
  344. ioapic->dev.in_range = ioapic_in_range;
  345. ioapic->dev.private = ioapic;
  346. ioapic->kvm = kvm;
  347. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  348. return 0;
  349. }