Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / virt / kvm / ioapic.c
1 /*
2  *  Copyright (C) 2001  MandrakeSoft S.A.
3  *  Copyright 2010 Red Hat, Inc. and/or its affiliates.
4  *
5  *    MandrakeSoft S.A.
6  *    43, rue d'Aboukir
7  *    75002 Paris - France
8  *    http://www.linux-mandrake.com/
9  *    http://www.mandrakesoft.com/
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Lesser General Public
13  *  License as published by the Free Software Foundation; either
14  *  version 2 of the License, or (at your option) any later version.
15  *
16  *  This library is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  Lesser General Public License for more details.
20  *
21  *  You should have received a copy of the GNU Lesser General Public
22  *  License along with this library; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24  *
25  *  Yunhong Jiang <yunhong.jiang@intel.com>
26  *  Yaozu (Eddie) Dong <eddie.dong@intel.com>
27  *  Based on Xen 3.1 code.
28  */
29
30 #include <linux/kvm_host.h>
31 #include <linux/kvm.h>
32 #include <linux/mm.h>
33 #include <linux/highmem.h>
34 #include <linux/smp.h>
35 #include <linux/hrtimer.h>
36 #include <linux/io.h>
37 #include <linux/slab.h>
38 #include <asm/processor.h>
39 #include <asm/page.h>
40 #include <asm/current.h>
41 #include <trace/events/kvm.h>
42
43 #include "ioapic.h"
44 #include "lapic.h"
45 #include "irq.h"
46
47 #if 0
48 #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
49 #else
50 #define ioapic_debug(fmt, arg...)
51 #endif
52 static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
53
54 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
55                                           unsigned long addr,
56                                           unsigned long length)
57 {
58         unsigned long result = 0;
59
60         switch (ioapic->ioregsel) {
61         case IOAPIC_REG_VERSION:
62                 result = ((((IOAPIC_NUM_PINS - 1) & 0xff) << 16)
63                           | (IOAPIC_VERSION_ID & 0xff));
64                 break;
65
66         case IOAPIC_REG_APIC_ID:
67         case IOAPIC_REG_ARB_ID:
68                 result = ((ioapic->id & 0xf) << 24);
69                 break;
70
71         default:
72                 {
73                         u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
74                         u64 redir_content;
75
76                         ASSERT(redir_index < IOAPIC_NUM_PINS);
77
78                         redir_content = ioapic->redirtbl[redir_index].bits;
79                         result = (ioapic->ioregsel & 0x1) ?
80                             (redir_content >> 32) & 0xffffffff :
81                             redir_content & 0xffffffff;
82                         break;
83                 }
84         }
85
86         return result;
87 }
88
89 static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
90 {
91         union kvm_ioapic_redirect_entry *pent;
92         int injected = -1;
93
94         pent = &ioapic->redirtbl[idx];
95
96         if (!pent->fields.mask) {
97                 injected = ioapic_deliver(ioapic, idx);
98                 if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
99                         pent->fields.remote_irr = 1;
100         }
101
102         return injected;
103 }
104
105 static void update_handled_vectors(struct kvm_ioapic *ioapic)
106 {
107         DECLARE_BITMAP(handled_vectors, 256);
108         int i;
109
110         memset(handled_vectors, 0, sizeof(handled_vectors));
111         for (i = 0; i < IOAPIC_NUM_PINS; ++i)
112                 __set_bit(ioapic->redirtbl[i].fields.vector, handled_vectors);
113         memcpy(ioapic->handled_vectors, handled_vectors,
114                sizeof(handled_vectors));
115         smp_wmb();
116 }
117
118 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
119 {
120         unsigned index;
121         bool mask_before, mask_after;
122         union kvm_ioapic_redirect_entry *e;
123
124         switch (ioapic->ioregsel) {
125         case IOAPIC_REG_VERSION:
126                 /* Writes are ignored. */
127                 break;
128
129         case IOAPIC_REG_APIC_ID:
130                 ioapic->id = (val >> 24) & 0xf;
131                 break;
132
133         case IOAPIC_REG_ARB_ID:
134                 break;
135
136         default:
137                 index = (ioapic->ioregsel - 0x10) >> 1;
138
139                 ioapic_debug("change redir index %x val %x\n", index, val);
140                 if (index >= IOAPIC_NUM_PINS)
141                         return;
142                 e = &ioapic->redirtbl[index];
143                 mask_before = e->fields.mask;
144                 if (ioapic->ioregsel & 1) {
145                         e->bits &= 0xffffffff;
146                         e->bits |= (u64) val << 32;
147                 } else {
148                         e->bits &= ~0xffffffffULL;
149                         e->bits |= (u32) val;
150                         e->fields.remote_irr = 0;
151                 }
152                 update_handled_vectors(ioapic);
153                 mask_after = e->fields.mask;
154                 if (mask_before != mask_after)
155                         kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
156                 if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
157                     && ioapic->irr & (1 << index))
158                         ioapic_service(ioapic, index);
159                 break;
160         }
161 }
162
163 static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
164 {
165         union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
166         struct kvm_lapic_irq irqe;
167
168         ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
169                      "vector=%x trig_mode=%x\n",
170                      entry->fields.dest_id, entry->fields.dest_mode,
171                      entry->fields.delivery_mode, entry->fields.vector,
172                      entry->fields.trig_mode);
173
174         irqe.dest_id = entry->fields.dest_id;
175         irqe.vector = entry->fields.vector;
176         irqe.dest_mode = entry->fields.dest_mode;
177         irqe.trig_mode = entry->fields.trig_mode;
178         irqe.delivery_mode = entry->fields.delivery_mode << 8;
179         irqe.level = 1;
180         irqe.shorthand = 0;
181
182 #ifdef CONFIG_X86
183         /* Always delivery PIT interrupt to vcpu 0 */
184         if (irq == 0) {
185                 irqe.dest_mode = 0; /* Physical mode. */
186                 /* need to read apic_id from apic regiest since
187                  * it can be rewritten */
188                 irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
189         }
190 #endif
191         return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
192 }
193
194 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
195 {
196         u32 old_irr;
197         u32 mask = 1 << irq;
198         union kvm_ioapic_redirect_entry entry;
199         int ret = 1;
200
201         spin_lock(&ioapic->lock);
202         old_irr = ioapic->irr;
203         if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
204                 entry = ioapic->redirtbl[irq];
205 // polarity is always active high in qemu
206 //              level ^= entry.fields.polarity;
207                 if (!level)
208                         ioapic->irr &= ~mask;
209                 else {
210                         int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
211                         ioapic->irr |= mask;
212                         if ((edge && old_irr != ioapic->irr) ||
213                             (!edge && !entry.fields.remote_irr))
214                                 ret = ioapic_service(ioapic, irq);
215                         else
216                                 ret = 0; /* report coalesced interrupt */
217                 }
218                 trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
219         }
220         spin_unlock(&ioapic->lock);
221
222         return ret;
223 }
224
225 static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
226                                      int trigger_mode)
227 {
228         int i;
229
230         for (i = 0; i < IOAPIC_NUM_PINS; i++) {
231                 union kvm_ioapic_redirect_entry *ent = &ioapic->redirtbl[i];
232
233                 if (ent->fields.vector != vector)
234                         continue;
235
236                 /*
237                  * We are dropping lock while calling ack notifiers because ack
238                  * notifier callbacks for assigned devices call into IOAPIC
239                  * recursively. Since remote_irr is cleared only after call
240                  * to notifiers if the same vector will be delivered while lock
241                  * is dropped it will be put into irr and will be delivered
242                  * after ack notifier returns.
243                  */
244                 spin_unlock(&ioapic->lock);
245                 kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
246                 spin_lock(&ioapic->lock);
247
248                 if (trigger_mode != IOAPIC_LEVEL_TRIG)
249                         continue;
250
251                 ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
252                 ent->fields.remote_irr = 0;
253                 if (!ent->fields.mask && (ioapic->irr & (1 << i)))
254                         ioapic_service(ioapic, i);
255         }
256 }
257
258 void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
259 {
260         struct kvm_ioapic *ioapic = kvm->arch.vioapic;
261
262         smp_rmb();
263         if (!test_bit(vector, ioapic->handled_vectors))
264                 return;
265         spin_lock(&ioapic->lock);
266         __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
267         spin_unlock(&ioapic->lock);
268 }
269
270 static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev)
271 {
272         return container_of(dev, struct kvm_ioapic, dev);
273 }
274
275 static inline int ioapic_in_range(struct kvm_ioapic *ioapic, gpa_t addr)
276 {
277         return ((addr >= ioapic->base_address &&
278                  (addr < ioapic->base_address + IOAPIC_MEM_LENGTH)));
279 }
280
281 static int ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len,
282                             void *val)
283 {
284         struct kvm_ioapic *ioapic = to_ioapic(this);
285         u32 result;
286         if (!ioapic_in_range(ioapic, addr))
287                 return -EOPNOTSUPP;
288
289         ioapic_debug("addr %lx\n", (unsigned long)addr);
290         ASSERT(!(addr & 0xf));  /* check alignment */
291
292         addr &= 0xff;
293         spin_lock(&ioapic->lock);
294         switch (addr) {
295         case IOAPIC_REG_SELECT:
296                 result = ioapic->ioregsel;
297                 break;
298
299         case IOAPIC_REG_WINDOW:
300                 result = ioapic_read_indirect(ioapic, addr, len);
301                 break;
302
303         default:
304                 result = 0;
305                 break;
306         }
307         spin_unlock(&ioapic->lock);
308
309         switch (len) {
310         case 8:
311                 *(u64 *) val = result;
312                 break;
313         case 1:
314         case 2:
315         case 4:
316                 memcpy(val, (char *)&result, len);
317                 break;
318         default:
319                 printk(KERN_WARNING "ioapic: wrong length %d\n", len);
320         }
321         return 0;
322 }
323
324 static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
325                              const void *val)
326 {
327         struct kvm_ioapic *ioapic = to_ioapic(this);
328         u32 data;
329         if (!ioapic_in_range(ioapic, addr))
330                 return -EOPNOTSUPP;
331
332         ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
333                      (void*)addr, len, val);
334         ASSERT(!(addr & 0xf));  /* check alignment */
335
336         switch (len) {
337         case 8:
338         case 4:
339                 data = *(u32 *) val;
340                 break;
341         case 2:
342                 data = *(u16 *) val;
343                 break;
344         case 1:
345                 data = *(u8  *) val;
346                 break;
347         default:
348                 printk(KERN_WARNING "ioapic: Unsupported size %d\n", len);
349                 return 0;
350         }
351
352         addr &= 0xff;
353         spin_lock(&ioapic->lock);
354         switch (addr) {
355         case IOAPIC_REG_SELECT:
356                 ioapic->ioregsel = data & 0xFF; /* 8-bit register */
357                 break;
358
359         case IOAPIC_REG_WINDOW:
360                 ioapic_write_indirect(ioapic, data);
361                 break;
362 #ifdef  CONFIG_IA64
363         case IOAPIC_REG_EOI:
364                 __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
365                 break;
366 #endif
367
368         default:
369                 break;
370         }
371         spin_unlock(&ioapic->lock);
372         return 0;
373 }
374
375 void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
376 {
377         int i;
378
379         for (i = 0; i < IOAPIC_NUM_PINS; i++)
380                 ioapic->redirtbl[i].fields.mask = 1;
381         ioapic->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
382         ioapic->ioregsel = 0;
383         ioapic->irr = 0;
384         ioapic->id = 0;
385         update_handled_vectors(ioapic);
386 }
387
388 static const struct kvm_io_device_ops ioapic_mmio_ops = {
389         .read     = ioapic_mmio_read,
390         .write    = ioapic_mmio_write,
391 };
392
393 int kvm_ioapic_init(struct kvm *kvm)
394 {
395         struct kvm_ioapic *ioapic;
396         int ret;
397
398         ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
399         if (!ioapic)
400                 return -ENOMEM;
401         spin_lock_init(&ioapic->lock);
402         kvm->arch.vioapic = ioapic;
403         kvm_ioapic_reset(ioapic);
404         kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
405         ioapic->kvm = kvm;
406         mutex_lock(&kvm->slots_lock);
407         ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
408                                       IOAPIC_MEM_LENGTH, &ioapic->dev);
409         mutex_unlock(&kvm->slots_lock);
410         if (ret < 0) {
411                 kvm->arch.vioapic = NULL;
412                 kfree(ioapic);
413         }
414
415         return ret;
416 }
417
418 void kvm_ioapic_destroy(struct kvm *kvm)
419 {
420         struct kvm_ioapic *ioapic = kvm->arch.vioapic;
421
422         if (ioapic) {
423                 kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
424                 kvm->arch.vioapic = NULL;
425                 kfree(ioapic);
426         }
427 }
428
429 int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
430 {
431         struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
432         if (!ioapic)
433                 return -EINVAL;
434
435         spin_lock(&ioapic->lock);
436         memcpy(state, ioapic, sizeof(struct kvm_ioapic_state));
437         spin_unlock(&ioapic->lock);
438         return 0;
439 }
440
441 int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state)
442 {
443         struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
444         if (!ioapic)
445                 return -EINVAL;
446
447         spin_lock(&ioapic->lock);
448         memcpy(ioapic, state, sizeof(struct kvm_ioapic_state));
449         update_handled_vectors(ioapic);
450         spin_unlock(&ioapic->lock);
451         return 0;
452 }