ioapic.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 "irq.h"
  39. #if 0
  40. #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
  41. #else
  42. #define ioapic_debug(fmt, arg...)
  43. #endif
  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\n", 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_vcpu *vcpu,
  117. u8 vector, u8 trig_mode, u8 delivery_mode)
  118. {
  119. ioapic_debug("irq %d trig %d deliv %d\n", vector, trig_mode,
  120. delivery_mode);
  121. ASSERT((delivery_mode == IOAPIC_FIXED) ||
  122. (delivery_mode == IOAPIC_LOWEST_PRIORITY));
  123. kvm_apic_set_irq(vcpu, 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\n", 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]->arch.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->arch.apic, dest)) {
  145. if (vcpu->arch.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->arch.apic &&
  156. kvm_apic_match_logical_addr(vcpu->arch.apic, dest))
  157. mask |= 1 << vcpu->vcpu_id;
  158. }
  159. ioapic_debug("mask %x\n", 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_vcpu *vcpu;
  171. int vcpu_id;
  172. ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
  173. "vector=%x trig_mode=%x\n",
  174. dest, dest_mode, delivery_mode, vector, trig_mode);
  175. deliver_bitmask = ioapic_get_delivery_bitmask(ioapic, dest, dest_mode);
  176. if (!deliver_bitmask) {
  177. ioapic_debug("no target on destination\n");
  178. return;
  179. }
  180. switch (delivery_mode) {
  181. case IOAPIC_LOWEST_PRIORITY:
  182. vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
  183. deliver_bitmask);
  184. if (vcpu != NULL)
  185. ioapic_inj_irq(ioapic, vcpu, vector,
  186. trig_mode, delivery_mode);
  187. else
  188. ioapic_debug("null lowest prio vcpu: "
  189. "mask=%x vector=%x delivery_mode=%x\n",
  190. deliver_bitmask, vector, IOAPIC_LOWEST_PRIORITY);
  191. break;
  192. case IOAPIC_FIXED:
  193. for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  194. if (!(deliver_bitmask & (1 << vcpu_id)))
  195. continue;
  196. deliver_bitmask &= ~(1 << vcpu_id);
  197. vcpu = ioapic->kvm->vcpus[vcpu_id];
  198. if (vcpu) {
  199. ioapic_inj_irq(ioapic, vcpu, vector,
  200. trig_mode, delivery_mode);
  201. }
  202. }
  203. break;
  204. /* TODO: NMI */
  205. default:
  206. printk(KERN_WARNING "Unsupported delivery mode %d\n",
  207. delivery_mode);
  208. break;
  209. }
  210. }
  211. void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
  212. {
  213. u32 old_irr = ioapic->irr;
  214. u32 mask = 1 << irq;
  215. union ioapic_redir_entry entry;
  216. if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
  217. entry = ioapic->redirtbl[irq];
  218. level ^= entry.fields.polarity;
  219. if (!level)
  220. ioapic->irr &= ~mask;
  221. else {
  222. ioapic->irr |= mask;
  223. if ((!entry.fields.trig_mode && old_irr != ioapic->irr)
  224. || !entry.fields.remote_irr)
  225. ioapic_service(ioapic, irq);
  226. }
  227. }
  228. }
  229. static int get_eoi_gsi(struct kvm_ioapic *ioapic, int vector)
  230. {
  231. int i;
  232. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  233. if (ioapic->redirtbl[i].fields.vector == vector)
  234. return i;
  235. return -1;
  236. }
  237. void kvm_ioapic_update_eoi(struct kvm *kvm, int vector)
  238. {
  239. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  240. union ioapic_redir_entry *ent;
  241. int gsi;
  242. gsi = get_eoi_gsi(ioapic, vector);
  243. if (gsi == -1) {
  244. printk(KERN_WARNING "Can't find redir item for %d EOI\n",
  245. vector);
  246. return;
  247. }
  248. ent = &ioapic->redirtbl[gsi];
  249. ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
  250. ent->fields.remote_irr = 0;
  251. if (!ent->fields.mask && (ioapic->irr & (1 << gsi)))
  252. ioapic_deliver(ioapic, gsi);
  253. }
  254. static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr)
  255. {
  256. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  257. return ((addr >= ioapic->base_address &&
  258. (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
  259. }
  260. static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
  261. void *val)
  262. {
  263. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  264. u32 result;
  265. ioapic_debug("addr %lx\n", (unsigned long)addr);
  266. ASSERT(!(addr & 0xf)); /* check alignment */
  267. addr &= 0xff;
  268. switch (addr) {
  269. case IOAPIC_REG_SELECT:
  270. result = ioapic->ioregsel;
  271. break;
  272. case IOAPIC_REG_WINDOW:
  273. result = ioapic_read_indirect(ioapic, addr, len);
  274. break;
  275. default:
  276. result = 0;
  277. break;
  278. }
  279. switch (len) {
  280. case 8:
  281. *(u64 *) val = result;
  282. break;
  283. case 1:
  284. case 2:
  285. case 4:
  286. memcpy(val, (char *)&result, len);
  287. break;
  288. default:
  289. printk(KERN_WARNING "ioapic: wrong length %d\n", len);
  290. }
  291. }
  292. static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
  293. const void *val)
  294. {
  295. struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private;
  296. u32 data;
  297. ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
  298. (void*)addr, len, val);
  299. ASSERT(!(addr & 0xf)); /* check alignment */
  300. if (len == 4 || len == 8)
  301. data = *(u32 *) val;
  302. else {
  303. printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
  304. return;
  305. }
  306. addr &= 0xff;
  307. switch (addr) {
  308. case IOAPIC_REG_SELECT:
  309. ioapic->ioregsel = data;
  310. break;
  311. case IOAPIC_REG_WINDOW:
  312. ioapic_write_indirect(ioapic, data);
  313. break;
  314. #ifdef CONFIG_IA64
  315. case IOAPIC_REG_EOI:
  316. kvm_ioapic_update_eoi(ioapic, data);
  317. break;
  318. #endif
  319. default:
  320. break;
  321. }
  322. }
  323. void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
  324. {
  325. int i;
  326. for (i = 0; i < IOAPIC_NUM_PINS; i++)
  327. ioapic->redirtbl[i].fields.mask = 1;
  328. ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
  329. ioapic->ioregsel = 0;
  330. ioapic->irr = 0;
  331. ioapic->id = 0;
  332. }
  333. int kvm_ioapic_init(struct kvm *kvm)
  334. {
  335. struct kvm_ioapic *ioapic;
  336. ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
  337. if (!ioapic)
  338. return -ENOMEM;
  339. kvm->arch.vioapic = ioapic;
  340. kvm_ioapic_reset(ioapic);
  341. ioapic->dev.read = ioapic_mmio_read;
  342. ioapic->dev.write = ioapic_mmio_write;
  343. ioapic->dev.in_range = ioapic_in_range;
  344. ioapic->dev.private = ioapic;
  345. ioapic->kvm = kvm;
  346. kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
  347. return 0;
  348. }