ioapic.c 9.5 KB

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