ioapic.c 9.5 KB

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