blob: eef9ccafdc098fb17c706d8382d1f0e6530fd43b [file] [log] [blame]
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001/*
2 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation.
7 */
8
9#define pr_fmt(fmt) "xive-kvm: " fmt
10
11#include <linux/kernel.h>
12#include <linux/kvm_host.h>
13#include <linux/err.h>
14#include <linux/gfp.h>
15#include <linux/spinlock.h>
16#include <linux/delay.h>
17#include <linux/percpu.h>
18#include <linux/cpumask.h>
19#include <asm/uaccess.h>
20#include <asm/kvm_book3s.h>
21#include <asm/kvm_ppc.h>
22#include <asm/hvcall.h>
23#include <asm/xics.h>
24#include <asm/xive.h>
25#include <asm/xive-regs.h>
26#include <asm/debug.h>
Paolo Bonzini4415b332017-05-09 11:50:01 +020027#include <asm/debugfs.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100028#include <asm/time.h>
29#include <asm/opal.h>
30
31#include <linux/debugfs.h>
32#include <linux/seq_file.h>
33
34#include "book3s_xive.h"
35
36
37/*
38 * Virtual mode variants of the hcalls for use on radix/radix
39 * with AIL. They require the VCPU's VP to be "pushed"
40 *
41 * We still instanciate them here because we use some of the
42 * generated utility functions as well in this file.
43 */
44#define XIVE_RUNTIME_CHECKS
45#define X_PFX xive_vm_
46#define X_STATIC static
47#define X_STAT_PFX stat_vm_
48#define __x_tima xive_tima
49#define __x_eoi_page(xd) ((void __iomem *)((xd)->eoi_mmio))
50#define __x_trig_page(xd) ((void __iomem *)((xd)->trig_mmio))
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100051#define __x_writeb __raw_writeb
52#define __x_readw __raw_readw
53#define __x_readq __raw_readq
54#define __x_writeq __raw_writeq
55
56#include "book3s_xive_template.c"
57
58/*
59 * We leave a gap of a couple of interrupts in the queue to
60 * account for the IPI and additional safety guard.
61 */
62#define XIVE_Q_GAP 2
63
64/*
65 * This is a simple trigger for a generic XIVE IRQ. This must
66 * only be called for interrupts that support a trigger page
67 */
68static bool xive_irq_trigger(struct xive_irq_data *xd)
69{
70 /* This should be only for MSIs */
71 if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
72 return false;
73
74 /* Those interrupts should always have a trigger page */
75 if (WARN_ON(!xd->trig_mmio))
76 return false;
77
78 out_be64(xd->trig_mmio, 0);
79
80 return true;
81}
82
83static irqreturn_t xive_esc_irq(int irq, void *data)
84{
85 struct kvm_vcpu *vcpu = data;
86
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +110087 vcpu->arch.irq_pending = 1;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100088 smp_mb();
89 if (vcpu->arch.ceded)
90 kvmppc_fast_vcpu_kick(vcpu);
91
92 return IRQ_HANDLED;
93}
94
95static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)
96{
97 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
98 struct xive_q *q = &xc->queues[prio];
99 char *name = NULL;
100 int rc;
101
102 /* Already there ? */
103 if (xc->esc_virq[prio])
104 return 0;
105
106 /* Hook up the escalation interrupt */
107 xc->esc_virq[prio] = irq_create_mapping(NULL, q->esc_irq);
108 if (!xc->esc_virq[prio]) {
109 pr_err("Failed to map escalation interrupt for queue %d of VCPU %d\n",
110 prio, xc->server_num);
111 return -EIO;
112 }
113
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100114 if (xc->xive->single_escalation)
115 name = kasprintf(GFP_KERNEL, "kvm-%d-%d",
116 vcpu->kvm->arch.lpid, xc->server_num);
117 else
118 name = kasprintf(GFP_KERNEL, "kvm-%d-%d-%d",
119 vcpu->kvm->arch.lpid, xc->server_num, prio);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000120 if (!name) {
121 pr_err("Failed to allocate escalation irq name for queue %d of VCPU %d\n",
122 prio, xc->server_num);
123 rc = -ENOMEM;
124 goto error;
125 }
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100126
127 pr_devel("Escalation %s irq %d (prio %d)\n", name, xc->esc_virq[prio], prio);
128
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000129 rc = request_irq(xc->esc_virq[prio], xive_esc_irq,
130 IRQF_NO_THREAD, name, vcpu);
131 if (rc) {
132 pr_err("Failed to request escalation interrupt for queue %d of VCPU %d\n",
133 prio, xc->server_num);
134 goto error;
135 }
136 xc->esc_virq_names[prio] = name;
137 return 0;
138error:
139 irq_dispose_mapping(xc->esc_virq[prio]);
140 xc->esc_virq[prio] = 0;
141 kfree(name);
142 return rc;
143}
144
145static int xive_provision_queue(struct kvm_vcpu *vcpu, u8 prio)
146{
147 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
148 struct kvmppc_xive *xive = xc->xive;
149 struct xive_q *q = &xc->queues[prio];
150 void *qpage;
151 int rc;
152
153 if (WARN_ON(q->qpage))
154 return 0;
155
156 /* Allocate the queue and retrieve infos on current node for now */
157 qpage = (__be32 *)__get_free_pages(GFP_KERNEL, xive->q_page_order);
158 if (!qpage) {
159 pr_err("Failed to allocate queue %d for VCPU %d\n",
160 prio, xc->server_num);
161 return -ENOMEM;;
162 }
163 memset(qpage, 0, 1 << xive->q_order);
164
165 /*
166 * Reconfigure the queue. This will set q->qpage only once the
167 * queue is fully configured. This is a requirement for prio 0
168 * as we will stop doing EOIs for every IPI as soon as we observe
169 * qpage being non-NULL, and instead will only EOI when we receive
170 * corresponding queue 0 entries
171 */
172 rc = xive_native_configure_queue(xc->vp_id, q, prio, qpage,
173 xive->q_order, true);
174 if (rc)
175 pr_err("Failed to configure queue %d for VCPU %d\n",
176 prio, xc->server_num);
177 return rc;
178}
179
180/* Called with kvm_lock held */
181static int xive_check_provisioning(struct kvm *kvm, u8 prio)
182{
183 struct kvmppc_xive *xive = kvm->arch.xive;
184 struct kvm_vcpu *vcpu;
185 int i, rc;
186
187 lockdep_assert_held(&kvm->lock);
188
189 /* Already provisioned ? */
190 if (xive->qmap & (1 << prio))
191 return 0;
192
193 pr_devel("Provisioning prio... %d\n", prio);
194
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100195 /* Provision each VCPU and enable escalations if needed */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000196 kvm_for_each_vcpu(i, vcpu, kvm) {
197 if (!vcpu->arch.xive_vcpu)
198 continue;
199 rc = xive_provision_queue(vcpu, prio);
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100200 if (rc == 0 && !xive->single_escalation)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000201 xive_attach_escalation(vcpu, prio);
202 if (rc)
203 return rc;
204 }
205
206 /* Order previous stores and mark it as provisioned */
207 mb();
208 xive->qmap |= (1 << prio);
209 return 0;
210}
211
212static void xive_inc_q_pending(struct kvm *kvm, u32 server, u8 prio)
213{
214 struct kvm_vcpu *vcpu;
215 struct kvmppc_xive_vcpu *xc;
216 struct xive_q *q;
217
218 /* Locate target server */
219 vcpu = kvmppc_xive_find_server(kvm, server);
220 if (!vcpu) {
221 pr_warn("%s: Can't find server %d\n", __func__, server);
222 return;
223 }
224 xc = vcpu->arch.xive_vcpu;
225 if (WARN_ON(!xc))
226 return;
227
228 q = &xc->queues[prio];
229 atomic_inc(&q->pending_count);
230}
231
232static int xive_try_pick_queue(struct kvm_vcpu *vcpu, u8 prio)
233{
234 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
235 struct xive_q *q;
236 u32 max;
237
238 if (WARN_ON(!xc))
239 return -ENXIO;
240 if (!xc->valid)
241 return -ENXIO;
242
243 q = &xc->queues[prio];
244 if (WARN_ON(!q->qpage))
245 return -ENXIO;
246
247 /* Calculate max number of interrupts in that queue. */
248 max = (q->msk + 1) - XIVE_Q_GAP;
249 return atomic_add_unless(&q->count, 1, max) ? 0 : -EBUSY;
250}
251
252static int xive_select_target(struct kvm *kvm, u32 *server, u8 prio)
253{
254 struct kvm_vcpu *vcpu;
255 int i, rc;
256
257 /* Locate target server */
258 vcpu = kvmppc_xive_find_server(kvm, *server);
259 if (!vcpu) {
260 pr_devel("Can't find server %d\n", *server);
261 return -EINVAL;
262 }
263
264 pr_devel("Finding irq target on 0x%x/%d...\n", *server, prio);
265
266 /* Try pick it */
267 rc = xive_try_pick_queue(vcpu, prio);
268 if (rc == 0)
269 return rc;
270
271 pr_devel(" .. failed, looking up candidate...\n");
272
273 /* Failed, pick another VCPU */
274 kvm_for_each_vcpu(i, vcpu, kvm) {
275 if (!vcpu->arch.xive_vcpu)
276 continue;
277 rc = xive_try_pick_queue(vcpu, prio);
278 if (rc == 0) {
279 *server = vcpu->arch.xive_vcpu->server_num;
280 pr_devel(" found on 0x%x/%d\n", *server, prio);
281 return rc;
282 }
283 }
284 pr_devel(" no available target !\n");
285
286 /* No available target ! */
287 return -EBUSY;
288}
289
290static u8 xive_lock_and_mask(struct kvmppc_xive *xive,
291 struct kvmppc_xive_src_block *sb,
292 struct kvmppc_xive_irq_state *state)
293{
294 struct xive_irq_data *xd;
295 u32 hw_num;
296 u8 old_prio;
297 u64 val;
298
299 /*
300 * Take the lock, set masked, try again if racing
301 * with H_EOI
302 */
303 for (;;) {
304 arch_spin_lock(&sb->lock);
305 old_prio = state->guest_priority;
306 state->guest_priority = MASKED;
307 mb();
308 if (!state->in_eoi)
309 break;
310 state->guest_priority = old_prio;
311 arch_spin_unlock(&sb->lock);
312 }
313
314 /* No change ? Bail */
315 if (old_prio == MASKED)
316 return old_prio;
317
318 /* Get the right irq */
319 kvmppc_xive_select_irq(state, &hw_num, &xd);
320
321 /*
322 * If the interrupt is marked as needing masking via
323 * firmware, we do it here. Firmware masking however
324 * is "lossy", it won't return the old p and q bits
325 * and won't set the interrupt to a state where it will
326 * record queued ones. If this is an issue we should do
327 * lazy masking instead.
328 *
329 * For now, we work around this in unmask by forcing
330 * an interrupt whenever we unmask a non-LSI via FW
331 * (if ever).
332 */
333 if (xd->flags & OPAL_XIVE_IRQ_MASK_VIA_FW) {
334 xive_native_configure_irq(hw_num,
335 xive->vp_base + state->act_server,
336 MASKED, state->number);
337 /* set old_p so we can track if an H_EOI was done */
338 state->old_p = true;
339 state->old_q = false;
340 } else {
341 /* Set PQ to 10, return old P and old Q and remember them */
342 val = xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_10);
343 state->old_p = !!(val & 2);
344 state->old_q = !!(val & 1);
345
346 /*
347 * Synchronize hardware to sensure the queues are updated
348 * when masking
349 */
350 xive_native_sync_source(hw_num);
351 }
352
353 return old_prio;
354}
355
356static void xive_lock_for_unmask(struct kvmppc_xive_src_block *sb,
357 struct kvmppc_xive_irq_state *state)
358{
359 /*
360 * Take the lock try again if racing with H_EOI
361 */
362 for (;;) {
363 arch_spin_lock(&sb->lock);
364 if (!state->in_eoi)
365 break;
366 arch_spin_unlock(&sb->lock);
367 }
368}
369
370static void xive_finish_unmask(struct kvmppc_xive *xive,
371 struct kvmppc_xive_src_block *sb,
372 struct kvmppc_xive_irq_state *state,
373 u8 prio)
374{
375 struct xive_irq_data *xd;
376 u32 hw_num;
377
378 /* If we aren't changing a thing, move on */
379 if (state->guest_priority != MASKED)
380 goto bail;
381
382 /* Get the right irq */
383 kvmppc_xive_select_irq(state, &hw_num, &xd);
384
385 /*
386 * See command in xive_lock_and_mask() concerning masking
387 * via firmware.
388 */
389 if (xd->flags & OPAL_XIVE_IRQ_MASK_VIA_FW) {
390 xive_native_configure_irq(hw_num,
391 xive->vp_base + state->act_server,
392 state->act_priority, state->number);
393 /* If an EOI is needed, do it here */
394 if (!state->old_p)
395 xive_vm_source_eoi(hw_num, xd);
396 /* If this is not an LSI, force a trigger */
397 if (!(xd->flags & OPAL_XIVE_IRQ_LSI))
398 xive_irq_trigger(xd);
399 goto bail;
400 }
401
402 /* Old Q set, set PQ to 11 */
403 if (state->old_q)
404 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_11);
405
406 /*
407 * If not old P, then perform an "effective" EOI,
408 * on the source. This will handle the cases where
409 * FW EOI is needed.
410 */
411 if (!state->old_p)
412 xive_vm_source_eoi(hw_num, xd);
413
414 /* Synchronize ordering and mark unmasked */
415 mb();
416bail:
417 state->guest_priority = prio;
418}
419
420/*
421 * Target an interrupt to a given server/prio, this will fallback
422 * to another server if necessary and perform the HW targetting
423 * updates as needed
424 *
425 * NOTE: Must be called with the state lock held
426 */
427static int xive_target_interrupt(struct kvm *kvm,
428 struct kvmppc_xive_irq_state *state,
429 u32 server, u8 prio)
430{
431 struct kvmppc_xive *xive = kvm->arch.xive;
432 u32 hw_num;
433 int rc;
434
435 /*
436 * This will return a tentative server and actual
437 * priority. The count for that new target will have
438 * already been incremented.
439 */
440 rc = xive_select_target(kvm, &server, prio);
441
442 /*
443 * We failed to find a target ? Not much we can do
444 * at least until we support the GIQ.
445 */
446 if (rc)
447 return rc;
448
449 /*
450 * Increment the old queue pending count if there
451 * was one so that the old queue count gets adjusted later
452 * when observed to be empty.
453 */
454 if (state->act_priority != MASKED)
455 xive_inc_q_pending(kvm,
456 state->act_server,
457 state->act_priority);
458 /*
459 * Update state and HW
460 */
461 state->act_priority = prio;
462 state->act_server = server;
463
464 /* Get the right irq */
465 kvmppc_xive_select_irq(state, &hw_num, NULL);
466
467 return xive_native_configure_irq(hw_num,
468 xive->vp_base + server,
469 prio, state->number);
470}
471
472/*
473 * Targetting rules: In order to avoid losing track of
474 * pending interrupts accross mask and unmask, which would
475 * allow queue overflows, we implement the following rules:
476 *
477 * - Unless it was never enabled (or we run out of capacity)
478 * an interrupt is always targetted at a valid server/queue
479 * pair even when "masked" by the guest. This pair tends to
480 * be the last one used but it can be changed under some
481 * circumstances. That allows us to separate targetting
482 * from masking, we only handle accounting during (re)targetting,
483 * this also allows us to let an interrupt drain into its target
484 * queue after masking, avoiding complex schemes to remove
485 * interrupts out of remote processor queues.
486 *
487 * - When masking, we set PQ to 10 and save the previous value
488 * of P and Q.
489 *
490 * - When unmasking, if saved Q was set, we set PQ to 11
491 * otherwise we leave PQ to the HW state which will be either
492 * 10 if nothing happened or 11 if the interrupt fired while
493 * masked. Effectively we are OR'ing the previous Q into the
494 * HW Q.
495 *
496 * Then if saved P is clear, we do an effective EOI (Q->P->Trigger)
497 * which will unmask the interrupt and shoot a new one if Q was
498 * set.
499 *
500 * Otherwise (saved P is set) we leave PQ unchanged (so 10 or 11,
501 * effectively meaning an H_EOI from the guest is still expected
502 * for that interrupt).
503 *
504 * - If H_EOI occurs while masked, we clear the saved P.
505 *
506 * - When changing target, we account on the new target and
507 * increment a separate "pending" counter on the old one.
508 * This pending counter will be used to decrement the old
509 * target's count when its queue has been observed empty.
510 */
511
512int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
513 u32 priority)
514{
515 struct kvmppc_xive *xive = kvm->arch.xive;
516 struct kvmppc_xive_src_block *sb;
517 struct kvmppc_xive_irq_state *state;
518 u8 new_act_prio;
519 int rc = 0;
520 u16 idx;
521
522 if (!xive)
523 return -ENODEV;
524
525 pr_devel("set_xive ! irq 0x%x server 0x%x prio %d\n",
526 irq, server, priority);
527
528 /* First, check provisioning of queues */
529 if (priority != MASKED)
530 rc = xive_check_provisioning(xive->kvm,
531 xive_prio_from_guest(priority));
532 if (rc) {
533 pr_devel(" provisioning failure %d !\n", rc);
534 return rc;
535 }
536
537 sb = kvmppc_xive_find_source(xive, irq, &idx);
538 if (!sb)
539 return -EINVAL;
540 state = &sb->irq_state[idx];
541
542 /*
543 * We first handle masking/unmasking since the locking
544 * might need to be retried due to EOIs, we'll handle
545 * targetting changes later. These functions will return
546 * with the SB lock held.
547 *
548 * xive_lock_and_mask() will also set state->guest_priority
549 * but won't otherwise change other fields of the state.
550 *
551 * xive_lock_for_unmask will not actually unmask, this will
552 * be done later by xive_finish_unmask() once the targetting
553 * has been done, so we don't try to unmask an interrupt
554 * that hasn't yet been targetted.
555 */
556 if (priority == MASKED)
557 xive_lock_and_mask(xive, sb, state);
558 else
559 xive_lock_for_unmask(sb, state);
560
561
562 /*
563 * Then we handle targetting.
564 *
565 * First calculate a new "actual priority"
566 */
567 new_act_prio = state->act_priority;
568 if (priority != MASKED)
569 new_act_prio = xive_prio_from_guest(priority);
570
571 pr_devel(" new_act_prio=%x act_server=%x act_prio=%x\n",
572 new_act_prio, state->act_server, state->act_priority);
573
574 /*
575 * Then check if we actually need to change anything,
576 *
577 * The condition for re-targetting the interrupt is that
578 * we have a valid new priority (new_act_prio is not 0xff)
579 * and either the server or the priority changed.
580 *
581 * Note: If act_priority was ff and the new priority is
582 * also ff, we don't do anything and leave the interrupt
583 * untargetted. An attempt of doing an int_on on an
584 * untargetted interrupt will fail. If that is a problem
585 * we could initialize interrupts with valid default
586 */
587
588 if (new_act_prio != MASKED &&
589 (state->act_server != server ||
590 state->act_priority != new_act_prio))
591 rc = xive_target_interrupt(kvm, state, server, new_act_prio);
592
593 /*
594 * Perform the final unmasking of the interrupt source
595 * if necessary
596 */
597 if (priority != MASKED)
598 xive_finish_unmask(xive, sb, state, priority);
599
600 /*
601 * Finally Update saved_priority to match. Only int_on/off
602 * set this field to a different value.
603 */
604 state->saved_priority = priority;
605
606 arch_spin_unlock(&sb->lock);
607 return rc;
608}
609
610int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
611 u32 *priority)
612{
613 struct kvmppc_xive *xive = kvm->arch.xive;
614 struct kvmppc_xive_src_block *sb;
615 struct kvmppc_xive_irq_state *state;
616 u16 idx;
617
618 if (!xive)
619 return -ENODEV;
620
621 sb = kvmppc_xive_find_source(xive, irq, &idx);
622 if (!sb)
623 return -EINVAL;
624 state = &sb->irq_state[idx];
625 arch_spin_lock(&sb->lock);
Sam Bobroff2fb1e942017-09-26 16:47:04 +1000626 *server = state->act_server;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000627 *priority = state->guest_priority;
628 arch_spin_unlock(&sb->lock);
629
630 return 0;
631}
632
633int kvmppc_xive_int_on(struct kvm *kvm, u32 irq)
634{
635 struct kvmppc_xive *xive = kvm->arch.xive;
636 struct kvmppc_xive_src_block *sb;
637 struct kvmppc_xive_irq_state *state;
638 u16 idx;
639
640 if (!xive)
641 return -ENODEV;
642
643 sb = kvmppc_xive_find_source(xive, irq, &idx);
644 if (!sb)
645 return -EINVAL;
646 state = &sb->irq_state[idx];
647
648 pr_devel("int_on(irq=0x%x)\n", irq);
649
650 /*
651 * Check if interrupt was not targetted
652 */
653 if (state->act_priority == MASKED) {
654 pr_devel("int_on on untargetted interrupt\n");
655 return -EINVAL;
656 }
657
658 /* If saved_priority is 0xff, do nothing */
659 if (state->saved_priority == MASKED)
660 return 0;
661
662 /*
663 * Lock and unmask it.
664 */
665 xive_lock_for_unmask(sb, state);
666 xive_finish_unmask(xive, sb, state, state->saved_priority);
667 arch_spin_unlock(&sb->lock);
668
669 return 0;
670}
671
672int kvmppc_xive_int_off(struct kvm *kvm, u32 irq)
673{
674 struct kvmppc_xive *xive = kvm->arch.xive;
675 struct kvmppc_xive_src_block *sb;
676 struct kvmppc_xive_irq_state *state;
677 u16 idx;
678
679 if (!xive)
680 return -ENODEV;
681
682 sb = kvmppc_xive_find_source(xive, irq, &idx);
683 if (!sb)
684 return -EINVAL;
685 state = &sb->irq_state[idx];
686
687 pr_devel("int_off(irq=0x%x)\n", irq);
688
689 /*
690 * Lock and mask
691 */
692 state->saved_priority = xive_lock_and_mask(xive, sb, state);
693 arch_spin_unlock(&sb->lock);
694
695 return 0;
696}
697
698static bool xive_restore_pending_irq(struct kvmppc_xive *xive, u32 irq)
699{
700 struct kvmppc_xive_src_block *sb;
701 struct kvmppc_xive_irq_state *state;
702 u16 idx;
703
704 sb = kvmppc_xive_find_source(xive, irq, &idx);
705 if (!sb)
706 return false;
707 state = &sb->irq_state[idx];
708 if (!state->valid)
709 return false;
710
711 /*
712 * Trigger the IPI. This assumes we never restore a pass-through
713 * interrupt which should be safe enough
714 */
715 xive_irq_trigger(&state->ipi_data);
716
717 return true;
718}
719
720u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu)
721{
722 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
723
724 if (!xc)
725 return 0;
726
727 /* Return the per-cpu state for state saving/migration */
728 return (u64)xc->cppr << KVM_REG_PPC_ICP_CPPR_SHIFT |
729 (u64)xc->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT;
730}
731
732int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
733{
734 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
735 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
736 u8 cppr, mfrr;
737 u32 xisr;
738
739 if (!xc || !xive)
740 return -ENOENT;
741
742 /* Grab individual state fields. We don't use pending_pri */
743 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
744 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
745 KVM_REG_PPC_ICP_XISR_MASK;
746 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
747
748 pr_devel("set_icp vcpu %d cppr=0x%x mfrr=0x%x xisr=0x%x\n",
749 xc->server_num, cppr, mfrr, xisr);
750
751 /*
752 * We can't update the state of a "pushed" VCPU, but that
753 * shouldn't happen.
754 */
755 if (WARN_ON(vcpu->arch.xive_pushed))
756 return -EIO;
757
758 /* Update VCPU HW saved state */
759 vcpu->arch.xive_saved_state.cppr = cppr;
760 xc->hw_cppr = xc->cppr = cppr;
761
762 /*
763 * Update MFRR state. If it's not 0xff, we mark the VCPU as
764 * having a pending MFRR change, which will re-evaluate the
765 * target. The VCPU will thus potentially get a spurious
766 * interrupt but that's not a big deal.
767 */
768 xc->mfrr = mfrr;
769 if (mfrr < cppr)
770 xive_irq_trigger(&xc->vp_ipi_data);
771
772 /*
773 * Now saved XIRR is "interesting". It means there's something in
774 * the legacy "1 element" queue... for an IPI we simply ignore it,
775 * as the MFRR restore will handle that. For anything else we need
776 * to force a resend of the source.
777 * However the source may not have been setup yet. If that's the
778 * case, we keep that info and increment a counter in the xive to
779 * tell subsequent xive_set_source() to go look.
780 */
781 if (xisr > XICS_IPI && !xive_restore_pending_irq(xive, xisr)) {
782 xc->delayed_irq = xisr;
783 xive->delayed_irqs++;
784 pr_devel(" xisr restore delayed\n");
785 }
786
787 return 0;
788}
789
790int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
791 struct irq_desc *host_desc)
792{
793 struct kvmppc_xive *xive = kvm->arch.xive;
794 struct kvmppc_xive_src_block *sb;
795 struct kvmppc_xive_irq_state *state;
796 struct irq_data *host_data = irq_desc_get_irq_data(host_desc);
797 unsigned int host_irq = irq_desc_get_irq(host_desc);
798 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(host_data);
799 u16 idx;
800 u8 prio;
801 int rc;
802
803 if (!xive)
804 return -ENODEV;
805
806 pr_devel("set_mapped girq 0x%lx host HW irq 0x%x...\n",guest_irq, hw_irq);
807
808 sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
809 if (!sb)
810 return -EINVAL;
811 state = &sb->irq_state[idx];
812
813 /*
814 * Mark the passed-through interrupt as going to a VCPU,
815 * this will prevent further EOIs and similar operations
816 * from the XIVE code. It will also mask the interrupt
817 * to either PQ=10 or 11 state, the latter if the interrupt
818 * is pending. This will allow us to unmask or retrigger it
819 * after routing it to the guest with a simple EOI.
820 *
821 * The "state" argument is a "token", all it needs is to be
822 * non-NULL to switch to passed-through or NULL for the
823 * other way around. We may not yet have an actual VCPU
824 * target here and we don't really care.
825 */
826 rc = irq_set_vcpu_affinity(host_irq, state);
827 if (rc) {
828 pr_err("Failed to set VCPU affinity for irq %d\n", host_irq);
829 return rc;
830 }
831
832 /*
833 * Mask and read state of IPI. We need to know if its P bit
834 * is set as that means it's potentially already using a
835 * queue entry in the target
836 */
837 prio = xive_lock_and_mask(xive, sb, state);
838 pr_devel(" old IPI prio %02x P:%d Q:%d\n", prio,
839 state->old_p, state->old_q);
840
841 /* Turn the IPI hard off */
842 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
843
844 /* Grab info about irq */
845 state->pt_number = hw_irq;
846 state->pt_data = irq_data_get_irq_handler_data(host_data);
847
848 /*
849 * Configure the IRQ to match the existing configuration of
850 * the IPI if it was already targetted. Otherwise this will
851 * mask the interrupt in a lossy way (act_priority is 0xff)
852 * which is fine for a never started interrupt.
853 */
854 xive_native_configure_irq(hw_irq,
855 xive->vp_base + state->act_server,
856 state->act_priority, state->number);
857
858 /*
859 * We do an EOI to enable the interrupt (and retrigger if needed)
860 * if the guest has the interrupt unmasked and the P bit was *not*
861 * set in the IPI. If it was set, we know a slot may still be in
862 * use in the target queue thus we have to wait for a guest
863 * originated EOI
864 */
865 if (prio != MASKED && !state->old_p)
866 xive_vm_source_eoi(hw_irq, state->pt_data);
867
868 /* Clear old_p/old_q as they are no longer relevant */
869 state->old_p = state->old_q = false;
870
871 /* Restore guest prio (unlocks EOI) */
872 mb();
873 state->guest_priority = prio;
874 arch_spin_unlock(&sb->lock);
875
876 return 0;
877}
878EXPORT_SYMBOL_GPL(kvmppc_xive_set_mapped);
879
880int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
881 struct irq_desc *host_desc)
882{
883 struct kvmppc_xive *xive = kvm->arch.xive;
884 struct kvmppc_xive_src_block *sb;
885 struct kvmppc_xive_irq_state *state;
886 unsigned int host_irq = irq_desc_get_irq(host_desc);
887 u16 idx;
888 u8 prio;
889 int rc;
890
891 if (!xive)
892 return -ENODEV;
893
894 pr_devel("clr_mapped girq 0x%lx...\n", guest_irq);
895
896 sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
897 if (!sb)
898 return -EINVAL;
899 state = &sb->irq_state[idx];
900
901 /*
902 * Mask and read state of IRQ. We need to know if its P bit
903 * is set as that means it's potentially already using a
904 * queue entry in the target
905 */
906 prio = xive_lock_and_mask(xive, sb, state);
907 pr_devel(" old IRQ prio %02x P:%d Q:%d\n", prio,
908 state->old_p, state->old_q);
909
910 /*
911 * If old_p is set, the interrupt is pending, we switch it to
912 * PQ=11. This will force a resend in the host so the interrupt
913 * isn't lost to whatver host driver may pick it up
914 */
915 if (state->old_p)
916 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_11);
917
918 /* Release the passed-through interrupt to the host */
919 rc = irq_set_vcpu_affinity(host_irq, NULL);
920 if (rc) {
921 pr_err("Failed to clr VCPU affinity for irq %d\n", host_irq);
922 return rc;
923 }
924
925 /* Forget about the IRQ */
926 state->pt_number = 0;
927 state->pt_data = NULL;
928
929 /* Reconfigure the IPI */
930 xive_native_configure_irq(state->ipi_number,
931 xive->vp_base + state->act_server,
932 state->act_priority, state->number);
933
934 /*
935 * If old_p is set (we have a queue entry potentially
936 * occupied) or the interrupt is masked, we set the IPI
937 * to PQ=10 state. Otherwise we just re-enable it (PQ=00).
938 */
939 if (prio == MASKED || state->old_p)
940 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_10);
941 else
942 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_00);
943
944 /* Restore guest prio (unlocks EOI) */
945 mb();
946 state->guest_priority = prio;
947 arch_spin_unlock(&sb->lock);
948
949 return 0;
950}
951EXPORT_SYMBOL_GPL(kvmppc_xive_clr_mapped);
952
953static void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)
954{
955 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
956 struct kvm *kvm = vcpu->kvm;
957 struct kvmppc_xive *xive = kvm->arch.xive;
958 int i, j;
959
960 for (i = 0; i <= xive->max_sbid; i++) {
961 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
962
963 if (!sb)
964 continue;
965 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
966 struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
967
968 if (!state->valid)
969 continue;
970 if (state->act_priority == MASKED)
971 continue;
972 if (state->act_server != xc->server_num)
973 continue;
974
975 /* Clean it up */
976 arch_spin_lock(&sb->lock);
977 state->act_priority = MASKED;
978 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
979 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
980 if (state->pt_number) {
981 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
982 xive_native_configure_irq(state->pt_number, 0, MASKED, 0);
983 }
984 arch_spin_unlock(&sb->lock);
985 }
986 }
987}
988
989void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
990{
991 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
992 struct kvmppc_xive *xive = xc->xive;
993 int i;
994
995 pr_devel("cleanup_vcpu(cpu=%d)\n", xc->server_num);
996
997 /* Ensure no interrupt is still routed to that VP */
998 xc->valid = false;
999 kvmppc_xive_disable_vcpu_interrupts(vcpu);
1000
1001 /* Mask the VP IPI */
1002 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01);
1003
1004 /* Disable the VP */
1005 xive_native_disable_vp(xc->vp_id);
1006
1007 /* Free the queues & associated interrupts */
1008 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1009 struct xive_q *q = &xc->queues[i];
1010
1011 /* Free the escalation irq */
1012 if (xc->esc_virq[i]) {
1013 free_irq(xc->esc_virq[i], vcpu);
1014 irq_dispose_mapping(xc->esc_virq[i]);
1015 kfree(xc->esc_virq_names[i]);
1016 }
1017 /* Free the queue */
1018 xive_native_disable_queue(xc->vp_id, q, i);
1019 if (q->qpage) {
1020 free_pages((unsigned long)q->qpage,
1021 xive->q_page_order);
1022 q->qpage = NULL;
1023 }
1024 }
1025
1026 /* Free the IPI */
1027 if (xc->vp_ipi) {
1028 xive_cleanup_irq_data(&xc->vp_ipi_data);
1029 xive_native_free_irq(xc->vp_ipi);
1030 }
1031 /* Free the VP */
1032 kfree(xc);
1033}
1034
1035int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
1036 struct kvm_vcpu *vcpu, u32 cpu)
1037{
1038 struct kvmppc_xive *xive = dev->private;
1039 struct kvmppc_xive_vcpu *xc;
1040 int i, r = -EBUSY;
1041
1042 pr_devel("connect_vcpu(cpu=%d)\n", cpu);
1043
1044 if (dev->ops != &kvm_xive_ops) {
1045 pr_devel("Wrong ops !\n");
1046 return -EPERM;
1047 }
1048 if (xive->kvm != vcpu->kvm)
1049 return -EPERM;
1050 if (vcpu->arch.irq_type)
1051 return -EBUSY;
1052 if (kvmppc_xive_find_server(vcpu->kvm, cpu)) {
1053 pr_devel("Duplicate !\n");
1054 return -EEXIST;
1055 }
1056 if (cpu >= KVM_MAX_VCPUS) {
1057 pr_devel("Out of bounds !\n");
1058 return -EINVAL;
1059 }
1060 xc = kzalloc(sizeof(*xc), GFP_KERNEL);
1061 if (!xc)
1062 return -ENOMEM;
1063
1064 /* We need to synchronize with queue provisioning */
1065 mutex_lock(&vcpu->kvm->lock);
1066 vcpu->arch.xive_vcpu = xc;
1067 xc->xive = xive;
1068 xc->vcpu = vcpu;
1069 xc->server_num = cpu;
1070 xc->vp_id = xive->vp_base + cpu;
1071 xc->mfrr = 0xff;
1072 xc->valid = true;
1073
1074 r = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
1075 if (r)
1076 goto bail;
1077
1078 /* Configure VCPU fields for use by assembly push/pull */
1079 vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
1080 vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
1081
1082 /* Allocate IPI */
1083 xc->vp_ipi = xive_native_alloc_irq();
1084 if (!xc->vp_ipi) {
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001085 pr_err("Failed to allocate xive irq for VCPU IPI\n");
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001086 r = -EIO;
1087 goto bail;
1088 }
1089 pr_devel(" IPI=0x%x\n", xc->vp_ipi);
1090
1091 r = xive_native_populate_irq_data(xc->vp_ipi, &xc->vp_ipi_data);
1092 if (r)
1093 goto bail;
1094
1095 /*
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001096 * Enable the VP first as the single escalation mode will
1097 * affect escalation interrupts numbering
1098 */
1099 r = xive_native_enable_vp(xc->vp_id, xive->single_escalation);
1100 if (r) {
1101 pr_err("Failed to enable VP in OPAL, err %d\n", r);
1102 goto bail;
1103 }
1104
1105 /*
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001106 * Initialize queues. Initially we set them all for no queueing
1107 * and we enable escalation for queue 0 only which we'll use for
1108 * our mfrr change notifications. If the VCPU is hot-plugged, we
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001109 * do handle provisioning however based on the existing "map"
1110 * of enabled queues.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001111 */
1112 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1113 struct xive_q *q = &xc->queues[i];
1114
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001115 /* Single escalation, no queue 7 */
1116 if (i == 7 && xive->single_escalation)
1117 break;
1118
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001119 /* Is queue already enabled ? Provision it */
1120 if (xive->qmap & (1 << i)) {
1121 r = xive_provision_queue(vcpu, i);
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001122 if (r == 0 && !xive->single_escalation)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001123 xive_attach_escalation(vcpu, i);
1124 if (r)
1125 goto bail;
1126 } else {
1127 r = xive_native_configure_queue(xc->vp_id,
1128 q, i, NULL, 0, true);
1129 if (r) {
1130 pr_err("Failed to configure queue %d for VCPU %d\n",
1131 i, cpu);
1132 goto bail;
1133 }
1134 }
1135 }
1136
1137 /* If not done above, attach priority 0 escalation */
1138 r = xive_attach_escalation(vcpu, 0);
1139 if (r)
1140 goto bail;
1141
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001142 /* Route the IPI */
1143 r = xive_native_configure_irq(xc->vp_ipi, xc->vp_id, 0, XICS_IPI);
1144 if (!r)
1145 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_00);
1146
1147bail:
1148 mutex_unlock(&vcpu->kvm->lock);
1149 if (r) {
1150 kvmppc_xive_cleanup_vcpu(vcpu);
1151 return r;
1152 }
1153
1154 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1155 return 0;
1156}
1157
1158/*
1159 * Scanning of queues before/after migration save
1160 */
1161static void xive_pre_save_set_queued(struct kvmppc_xive *xive, u32 irq)
1162{
1163 struct kvmppc_xive_src_block *sb;
1164 struct kvmppc_xive_irq_state *state;
1165 u16 idx;
1166
1167 sb = kvmppc_xive_find_source(xive, irq, &idx);
1168 if (!sb)
1169 return;
1170
1171 state = &sb->irq_state[idx];
1172
1173 /* Some sanity checking */
1174 if (!state->valid) {
1175 pr_err("invalid irq 0x%x in cpu queue!\n", irq);
1176 return;
1177 }
1178
1179 /*
1180 * If the interrupt is in a queue it should have P set.
1181 * We warn so that gets reported. A backtrace isn't useful
1182 * so no need to use a WARN_ON.
1183 */
1184 if (!state->saved_p)
1185 pr_err("Interrupt 0x%x is marked in a queue but P not set !\n", irq);
1186
1187 /* Set flag */
1188 state->in_queue = true;
1189}
1190
1191static void xive_pre_save_mask_irq(struct kvmppc_xive *xive,
1192 struct kvmppc_xive_src_block *sb,
1193 u32 irq)
1194{
1195 struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
1196
1197 if (!state->valid)
1198 return;
1199
1200 /* Mask and save state, this will also sync HW queues */
1201 state->saved_scan_prio = xive_lock_and_mask(xive, sb, state);
1202
1203 /* Transfer P and Q */
1204 state->saved_p = state->old_p;
1205 state->saved_q = state->old_q;
1206
1207 /* Unlock */
1208 arch_spin_unlock(&sb->lock);
1209}
1210
1211static void xive_pre_save_unmask_irq(struct kvmppc_xive *xive,
1212 struct kvmppc_xive_src_block *sb,
1213 u32 irq)
1214{
1215 struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
1216
1217 if (!state->valid)
1218 return;
1219
1220 /*
1221 * Lock / exclude EOI (not technically necessary if the
1222 * guest isn't running concurrently. If this becomes a
1223 * performance issue we can probably remove the lock.
1224 */
1225 xive_lock_for_unmask(sb, state);
1226
1227 /* Restore mask/prio if it wasn't masked */
1228 if (state->saved_scan_prio != MASKED)
1229 xive_finish_unmask(xive, sb, state, state->saved_scan_prio);
1230
1231 /* Unlock */
1232 arch_spin_unlock(&sb->lock);
1233}
1234
1235static void xive_pre_save_queue(struct kvmppc_xive *xive, struct xive_q *q)
1236{
1237 u32 idx = q->idx;
1238 u32 toggle = q->toggle;
1239 u32 irq;
1240
1241 do {
1242 irq = __xive_read_eq(q->qpage, q->msk, &idx, &toggle);
1243 if (irq > XICS_IPI)
1244 xive_pre_save_set_queued(xive, irq);
1245 } while(irq);
1246}
1247
1248static void xive_pre_save_scan(struct kvmppc_xive *xive)
1249{
1250 struct kvm_vcpu *vcpu = NULL;
1251 int i, j;
1252
1253 /*
1254 * See comment in xive_get_source() about how this
1255 * work. Collect a stable state for all interrupts
1256 */
1257 for (i = 0; i <= xive->max_sbid; i++) {
1258 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1259 if (!sb)
1260 continue;
1261 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
1262 xive_pre_save_mask_irq(xive, sb, j);
1263 }
1264
1265 /* Then scan the queues and update the "in_queue" flag */
1266 kvm_for_each_vcpu(i, vcpu, xive->kvm) {
1267 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1268 if (!xc)
1269 continue;
1270 for (j = 0; j < KVMPPC_XIVE_Q_COUNT; j++) {
Paul Mackerras00c14752017-06-30 16:39:55 +10001271 if (xc->queues[j].qpage)
1272 xive_pre_save_queue(xive, &xc->queues[j]);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001273 }
1274 }
1275
1276 /* Finally restore interrupt states */
1277 for (i = 0; i <= xive->max_sbid; i++) {
1278 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1279 if (!sb)
1280 continue;
1281 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
1282 xive_pre_save_unmask_irq(xive, sb, j);
1283 }
1284}
1285
1286static void xive_post_save_scan(struct kvmppc_xive *xive)
1287{
1288 u32 i, j;
1289
1290 /* Clear all the in_queue flags */
1291 for (i = 0; i <= xive->max_sbid; i++) {
1292 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1293 if (!sb)
1294 continue;
1295 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
1296 sb->irq_state[j].in_queue = false;
1297 }
1298
1299 /* Next get_source() will do a new scan */
1300 xive->saved_src_count = 0;
1301}
1302
1303/*
1304 * This returns the source configuration and state to user space.
1305 */
1306static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr)
1307{
1308 struct kvmppc_xive_src_block *sb;
1309 struct kvmppc_xive_irq_state *state;
1310 u64 __user *ubufp = (u64 __user *) addr;
1311 u64 val, prio;
1312 u16 idx;
1313
1314 sb = kvmppc_xive_find_source(xive, irq, &idx);
1315 if (!sb)
1316 return -ENOENT;
1317
1318 state = &sb->irq_state[idx];
1319
1320 if (!state->valid)
1321 return -ENOENT;
1322
1323 pr_devel("get_source(%ld)...\n", irq);
1324
1325 /*
1326 * So to properly save the state into something that looks like a
1327 * XICS migration stream we cannot treat interrupts individually.
1328 *
1329 * We need, instead, mask them all (& save their previous PQ state)
1330 * to get a stable state in the HW, then sync them to ensure that
1331 * any interrupt that had already fired hits its queue, and finally
1332 * scan all the queues to collect which interrupts are still present
1333 * in the queues, so we can set the "pending" flag on them and
1334 * they can be resent on restore.
1335 *
1336 * So we do it all when the "first" interrupt gets saved, all the
1337 * state is collected at that point, the rest of xive_get_source()
1338 * will merely collect and convert that state to the expected
1339 * userspace bit mask.
1340 */
1341 if (xive->saved_src_count == 0)
1342 xive_pre_save_scan(xive);
1343 xive->saved_src_count++;
1344
1345 /* Convert saved state into something compatible with xics */
Sam Bobroff2fb1e942017-09-26 16:47:04 +10001346 val = state->act_server;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001347 prio = state->saved_scan_prio;
1348
1349 if (prio == MASKED) {
1350 val |= KVM_XICS_MASKED;
1351 prio = state->saved_priority;
1352 }
1353 val |= prio << KVM_XICS_PRIORITY_SHIFT;
1354 if (state->lsi) {
1355 val |= KVM_XICS_LEVEL_SENSITIVE;
1356 if (state->saved_p)
1357 val |= KVM_XICS_PENDING;
1358 } else {
1359 if (state->saved_p)
1360 val |= KVM_XICS_PRESENTED;
1361
1362 if (state->saved_q)
1363 val |= KVM_XICS_QUEUED;
1364
1365 /*
1366 * We mark it pending (which will attempt a re-delivery)
1367 * if we are in a queue *or* we were masked and had
1368 * Q set which is equivalent to the XICS "masked pending"
1369 * state
1370 */
1371 if (state->in_queue || (prio == MASKED && state->saved_q))
1372 val |= KVM_XICS_PENDING;
1373 }
1374
1375 /*
1376 * If that was the last interrupt saved, reset the
1377 * in_queue flags
1378 */
1379 if (xive->saved_src_count == xive->src_count)
1380 xive_post_save_scan(xive);
1381
1382 /* Copy the result to userspace */
1383 if (put_user(val, ubufp))
1384 return -EFAULT;
1385
1386 return 0;
1387}
1388
1389static struct kvmppc_xive_src_block *xive_create_src_block(struct kvmppc_xive *xive,
1390 int irq)
1391{
1392 struct kvm *kvm = xive->kvm;
1393 struct kvmppc_xive_src_block *sb;
1394 int i, bid;
1395
1396 bid = irq >> KVMPPC_XICS_ICS_SHIFT;
1397
1398 mutex_lock(&kvm->lock);
1399
1400 /* block already exists - somebody else got here first */
1401 if (xive->src_blocks[bid])
1402 goto out;
1403
1404 /* Create the ICS */
1405 sb = kzalloc(sizeof(*sb), GFP_KERNEL);
1406 if (!sb)
1407 goto out;
1408
1409 sb->id = bid;
1410
1411 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1412 sb->irq_state[i].number = (bid << KVMPPC_XICS_ICS_SHIFT) | i;
1413 sb->irq_state[i].guest_priority = MASKED;
1414 sb->irq_state[i].saved_priority = MASKED;
1415 sb->irq_state[i].act_priority = MASKED;
1416 }
1417 smp_wmb();
1418 xive->src_blocks[bid] = sb;
1419
1420 if (bid > xive->max_sbid)
1421 xive->max_sbid = bid;
1422
1423out:
1424 mutex_unlock(&kvm->lock);
1425 return xive->src_blocks[bid];
1426}
1427
1428static bool xive_check_delayed_irq(struct kvmppc_xive *xive, u32 irq)
1429{
1430 struct kvm *kvm = xive->kvm;
1431 struct kvm_vcpu *vcpu = NULL;
1432 int i;
1433
1434 kvm_for_each_vcpu(i, vcpu, kvm) {
1435 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1436
1437 if (!xc)
1438 continue;
1439
1440 if (xc->delayed_irq == irq) {
1441 xc->delayed_irq = 0;
1442 xive->delayed_irqs--;
1443 return true;
1444 }
1445 }
1446 return false;
1447}
1448
1449static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr)
1450{
1451 struct kvmppc_xive_src_block *sb;
1452 struct kvmppc_xive_irq_state *state;
1453 u64 __user *ubufp = (u64 __user *) addr;
1454 u16 idx;
1455 u64 val;
1456 u8 act_prio, guest_prio;
1457 u32 server;
1458 int rc = 0;
1459
1460 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1461 return -ENOENT;
1462
1463 pr_devel("set_source(irq=0x%lx)\n", irq);
1464
1465 /* Find the source */
1466 sb = kvmppc_xive_find_source(xive, irq, &idx);
1467 if (!sb) {
1468 pr_devel("No source, creating source block...\n");
1469 sb = xive_create_src_block(xive, irq);
1470 if (!sb) {
1471 pr_devel("Failed to create block...\n");
1472 return -ENOMEM;
1473 }
1474 }
1475 state = &sb->irq_state[idx];
1476
1477 /* Read user passed data */
1478 if (get_user(val, ubufp)) {
1479 pr_devel("fault getting user info !\n");
1480 return -EFAULT;
1481 }
1482
1483 server = val & KVM_XICS_DESTINATION_MASK;
1484 guest_prio = val >> KVM_XICS_PRIORITY_SHIFT;
1485
1486 pr_devel(" val=0x016%llx (server=0x%x, guest_prio=%d)\n",
1487 val, server, guest_prio);
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001488
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001489 /*
1490 * If the source doesn't already have an IPI, allocate
1491 * one and get the corresponding data
1492 */
1493 if (!state->ipi_number) {
1494 state->ipi_number = xive_native_alloc_irq();
1495 if (state->ipi_number == 0) {
1496 pr_devel("Failed to allocate IPI !\n");
1497 return -ENOMEM;
1498 }
1499 xive_native_populate_irq_data(state->ipi_number, &state->ipi_data);
1500 pr_devel(" src_ipi=0x%x\n", state->ipi_number);
1501 }
1502
1503 /*
1504 * We use lock_and_mask() to set us in the right masked
1505 * state. We will override that state from the saved state
1506 * further down, but this will handle the cases of interrupts
1507 * that need FW masking. We set the initial guest_priority to
1508 * 0 before calling it to ensure it actually performs the masking.
1509 */
1510 state->guest_priority = 0;
1511 xive_lock_and_mask(xive, sb, state);
1512
1513 /*
1514 * Now, we select a target if we have one. If we don't we
1515 * leave the interrupt untargetted. It means that an interrupt
1516 * can become "untargetted" accross migration if it was masked
1517 * by set_xive() but there is little we can do about it.
1518 */
1519
1520 /* First convert prio and mark interrupt as untargetted */
1521 act_prio = xive_prio_from_guest(guest_prio);
1522 state->act_priority = MASKED;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001523
1524 /*
1525 * We need to drop the lock due to the mutex below. Hopefully
1526 * nothing is touching that interrupt yet since it hasn't been
1527 * advertized to a running guest yet
1528 */
1529 arch_spin_unlock(&sb->lock);
1530
1531 /* If we have a priority target the interrupt */
1532 if (act_prio != MASKED) {
1533 /* First, check provisioning of queues */
1534 mutex_lock(&xive->kvm->lock);
1535 rc = xive_check_provisioning(xive->kvm, act_prio);
1536 mutex_unlock(&xive->kvm->lock);
1537
1538 /* Target interrupt */
1539 if (rc == 0)
1540 rc = xive_target_interrupt(xive->kvm, state,
1541 server, act_prio);
1542 /*
1543 * If provisioning or targetting failed, leave it
1544 * alone and masked. It will remain disabled until
1545 * the guest re-targets it.
1546 */
1547 }
1548
1549 /*
1550 * Find out if this was a delayed irq stashed in an ICP,
1551 * in which case, treat it as pending
1552 */
1553 if (xive->delayed_irqs && xive_check_delayed_irq(xive, irq)) {
1554 val |= KVM_XICS_PENDING;
1555 pr_devel(" Found delayed ! forcing PENDING !\n");
1556 }
1557
1558 /* Cleanup the SW state */
1559 state->old_p = false;
1560 state->old_q = false;
1561 state->lsi = false;
1562 state->asserted = false;
1563
1564 /* Restore LSI state */
1565 if (val & KVM_XICS_LEVEL_SENSITIVE) {
1566 state->lsi = true;
1567 if (val & KVM_XICS_PENDING)
1568 state->asserted = true;
1569 pr_devel(" LSI ! Asserted=%d\n", state->asserted);
1570 }
1571
1572 /*
1573 * Restore P and Q. If the interrupt was pending, we
1574 * force both P and Q, which will trigger a resend.
1575 *
1576 * That means that a guest that had both an interrupt
1577 * pending (queued) and Q set will restore with only
1578 * one instance of that interrupt instead of 2, but that
1579 * is perfectly fine as coalescing interrupts that haven't
1580 * been presented yet is always allowed.
1581 */
1582 if (val & KVM_XICS_PRESENTED || val & KVM_XICS_PENDING)
1583 state->old_p = true;
1584 if (val & KVM_XICS_QUEUED || val & KVM_XICS_PENDING)
1585 state->old_q = true;
1586
1587 pr_devel(" P=%d, Q=%d\n", state->old_p, state->old_q);
1588
1589 /*
1590 * If the interrupt was unmasked, update guest priority and
1591 * perform the appropriate state transition and do a
1592 * re-trigger if necessary.
1593 */
1594 if (val & KVM_XICS_MASKED) {
1595 pr_devel(" masked, saving prio\n");
1596 state->guest_priority = MASKED;
1597 state->saved_priority = guest_prio;
1598 } else {
1599 pr_devel(" unmasked, restoring to prio %d\n", guest_prio);
1600 xive_finish_unmask(xive, sb, state, guest_prio);
1601 state->saved_priority = guest_prio;
1602 }
1603
1604 /* Increment the number of valid sources and mark this one valid */
1605 if (!state->valid)
1606 xive->src_count++;
1607 state->valid = true;
1608
1609 return 0;
1610}
1611
1612int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1613 bool line_status)
1614{
1615 struct kvmppc_xive *xive = kvm->arch.xive;
1616 struct kvmppc_xive_src_block *sb;
1617 struct kvmppc_xive_irq_state *state;
1618 u16 idx;
1619
1620 if (!xive)
1621 return -ENODEV;
1622
1623 sb = kvmppc_xive_find_source(xive, irq, &idx);
1624 if (!sb)
1625 return -EINVAL;
1626
1627 /* Perform locklessly .... (we need to do some RCUisms here...) */
1628 state = &sb->irq_state[idx];
1629 if (!state->valid)
1630 return -EINVAL;
1631
1632 /* We don't allow a trigger on a passed-through interrupt */
1633 if (state->pt_number)
1634 return -EINVAL;
1635
1636 if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL)
1637 state->asserted = 1;
1638 else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
1639 state->asserted = 0;
1640 return 0;
1641 }
1642
1643 /* Trigger the IPI */
1644 xive_irq_trigger(&state->ipi_data);
1645
1646 return 0;
1647}
1648
1649static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1650{
1651 struct kvmppc_xive *xive = dev->private;
1652
1653 /* We honor the existing XICS ioctl */
1654 switch (attr->group) {
1655 case KVM_DEV_XICS_GRP_SOURCES:
1656 return xive_set_source(xive, attr->attr, attr->addr);
1657 }
1658 return -ENXIO;
1659}
1660
1661static int xive_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1662{
1663 struct kvmppc_xive *xive = dev->private;
1664
1665 /* We honor the existing XICS ioctl */
1666 switch (attr->group) {
1667 case KVM_DEV_XICS_GRP_SOURCES:
1668 return xive_get_source(xive, attr->attr, attr->addr);
1669 }
1670 return -ENXIO;
1671}
1672
1673static int xive_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1674{
1675 /* We honor the same limits as XICS, at least for now */
1676 switch (attr->group) {
1677 case KVM_DEV_XICS_GRP_SOURCES:
1678 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1679 attr->attr < KVMPPC_XICS_NR_IRQS)
1680 return 0;
1681 break;
1682 }
1683 return -ENXIO;
1684}
1685
1686static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd)
1687{
1688 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
1689 xive_native_configure_irq(hw_num, 0, MASKED, 0);
1690 xive_cleanup_irq_data(xd);
1691}
1692
1693static void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb)
1694{
1695 int i;
1696
1697 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1698 struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
1699
1700 if (!state->valid)
1701 continue;
1702
1703 kvmppc_xive_cleanup_irq(state->ipi_number, &state->ipi_data);
1704 xive_native_free_irq(state->ipi_number);
1705
1706 /* Pass-through, cleanup too */
1707 if (state->pt_number)
1708 kvmppc_xive_cleanup_irq(state->pt_number, state->pt_data);
1709
1710 state->valid = false;
1711 }
1712}
1713
1714static void kvmppc_xive_free(struct kvm_device *dev)
1715{
1716 struct kvmppc_xive *xive = dev->private;
1717 struct kvm *kvm = xive->kvm;
1718 int i;
1719
1720 debugfs_remove(xive->dentry);
1721
1722 if (kvm)
1723 kvm->arch.xive = NULL;
1724
1725 /* Mask and free interrupts */
1726 for (i = 0; i <= xive->max_sbid; i++) {
1727 if (xive->src_blocks[i])
1728 kvmppc_xive_free_sources(xive->src_blocks[i]);
1729 kfree(xive->src_blocks[i]);
1730 xive->src_blocks[i] = NULL;
1731 }
1732
1733 if (xive->vp_base != XIVE_INVALID_VP)
1734 xive_native_free_vp_block(xive->vp_base);
1735
1736
1737 kfree(xive);
1738 kfree(dev);
1739}
1740
1741static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
1742{
1743 struct kvmppc_xive *xive;
1744 struct kvm *kvm = dev->kvm;
1745 int ret = 0;
1746
1747 pr_devel("Creating xive for partition\n");
1748
1749 xive = kzalloc(sizeof(*xive), GFP_KERNEL);
1750 if (!xive)
1751 return -ENOMEM;
1752
1753 dev->private = xive;
1754 xive->dev = dev;
1755 xive->kvm = kvm;
1756
1757 /* Already there ? */
1758 if (kvm->arch.xive)
1759 ret = -EEXIST;
1760 else
1761 kvm->arch.xive = xive;
1762
1763 /* We use the default queue size set by the host */
1764 xive->q_order = xive_native_default_eq_shift();
1765 if (xive->q_order < PAGE_SHIFT)
1766 xive->q_page_order = 0;
1767 else
1768 xive->q_page_order = xive->q_order - PAGE_SHIFT;
1769
1770 /* Allocate a bunch of VPs */
1771 xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS);
1772 pr_devel("VP_Base=%x\n", xive->vp_base);
1773
1774 if (xive->vp_base == XIVE_INVALID_VP)
1775 ret = -ENOMEM;
1776
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001777 xive->single_escalation = xive_native_has_single_escalation();
1778
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001779 if (ret) {
1780 kfree(xive);
1781 return ret;
1782 }
1783
1784 return 0;
1785}
1786
1787
1788static int xive_debug_show(struct seq_file *m, void *private)
1789{
1790 struct kvmppc_xive *xive = m->private;
1791 struct kvm *kvm = xive->kvm;
1792 struct kvm_vcpu *vcpu;
1793 u64 t_rm_h_xirr = 0;
1794 u64 t_rm_h_ipoll = 0;
1795 u64 t_rm_h_cppr = 0;
1796 u64 t_rm_h_eoi = 0;
1797 u64 t_rm_h_ipi = 0;
1798 u64 t_vm_h_xirr = 0;
1799 u64 t_vm_h_ipoll = 0;
1800 u64 t_vm_h_cppr = 0;
1801 u64 t_vm_h_eoi = 0;
1802 u64 t_vm_h_ipi = 0;
1803 unsigned int i;
1804
1805 if (!kvm)
1806 return 0;
1807
1808 seq_printf(m, "=========\nVCPU state\n=========\n");
1809
1810 kvm_for_each_vcpu(i, vcpu, kvm) {
1811 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
Benjamin Herrenschmidtc424c102018-01-12 13:37:11 +11001812 unsigned int i;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001813
1814 if (!xc)
1815 continue;
1816
1817 seq_printf(m, "cpu server %#x CPPR:%#x HWCPPR:%#x"
1818 " MFRR:%#x PEND:%#x h_xirr: R=%lld V=%lld\n",
1819 xc->server_num, xc->cppr, xc->hw_cppr,
1820 xc->mfrr, xc->pending,
1821 xc->stat_rm_h_xirr, xc->stat_vm_h_xirr);
Benjamin Herrenschmidtc424c102018-01-12 13:37:11 +11001822 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1823 struct xive_q *q = &xc->queues[i];
1824 u32 i0, i1, idx;
1825
1826 if (!q->qpage && !xc->esc_virq[i])
1827 continue;
1828
1829 seq_printf(m, " [q%d]: ", i);
1830
1831 if (q->qpage) {
1832 idx = q->idx;
1833 i0 = be32_to_cpup(q->qpage + idx);
1834 idx = (idx + 1) & q->msk;
1835 i1 = be32_to_cpup(q->qpage + idx);
1836 seq_printf(m, "T=%d %08x %08x... \n", q->toggle, i0, i1);
1837 }
1838 if (xc->esc_virq[i]) {
1839 struct irq_data *d = irq_get_irq_data(xc->esc_virq[i]);
1840 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
1841 u64 pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
1842 seq_printf(m, "E:%c%c I(%d:%llx:%llx)",
1843 (pq & XIVE_ESB_VAL_P) ? 'P' : 'p',
1844 (pq & XIVE_ESB_VAL_Q) ? 'Q' : 'q',
1845 xc->esc_virq[i], pq, xd->eoi_page);
1846 seq_printf(m, "\n");
1847 }
1848 }
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001849
1850 t_rm_h_xirr += xc->stat_rm_h_xirr;
1851 t_rm_h_ipoll += xc->stat_rm_h_ipoll;
1852 t_rm_h_cppr += xc->stat_rm_h_cppr;
1853 t_rm_h_eoi += xc->stat_rm_h_eoi;
1854 t_rm_h_ipi += xc->stat_rm_h_ipi;
1855 t_vm_h_xirr += xc->stat_vm_h_xirr;
1856 t_vm_h_ipoll += xc->stat_vm_h_ipoll;
1857 t_vm_h_cppr += xc->stat_vm_h_cppr;
1858 t_vm_h_eoi += xc->stat_vm_h_eoi;
1859 t_vm_h_ipi += xc->stat_vm_h_ipi;
1860 }
1861
1862 seq_printf(m, "Hcalls totals\n");
1863 seq_printf(m, " H_XIRR R=%10lld V=%10lld\n", t_rm_h_xirr, t_vm_h_xirr);
1864 seq_printf(m, " H_IPOLL R=%10lld V=%10lld\n", t_rm_h_ipoll, t_vm_h_ipoll);
1865 seq_printf(m, " H_CPPR R=%10lld V=%10lld\n", t_rm_h_cppr, t_vm_h_cppr);
1866 seq_printf(m, " H_EOI R=%10lld V=%10lld\n", t_rm_h_eoi, t_vm_h_eoi);
1867 seq_printf(m, " H_IPI R=%10lld V=%10lld\n", t_rm_h_ipi, t_vm_h_ipi);
1868
1869 return 0;
1870}
1871
1872static int xive_debug_open(struct inode *inode, struct file *file)
1873{
1874 return single_open(file, xive_debug_show, inode->i_private);
1875}
1876
1877static const struct file_operations xive_debug_fops = {
1878 .open = xive_debug_open,
1879 .read = seq_read,
1880 .llseek = seq_lseek,
1881 .release = single_release,
1882};
1883
1884static void xive_debugfs_init(struct kvmppc_xive *xive)
1885{
1886 char *name;
1887
1888 name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
1889 if (!name) {
1890 pr_err("%s: no memory for name\n", __func__);
1891 return;
1892 }
1893
1894 xive->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
1895 xive, &xive_debug_fops);
1896
1897 pr_debug("%s: created %s\n", __func__, name);
1898 kfree(name);
1899}
1900
1901static void kvmppc_xive_init(struct kvm_device *dev)
1902{
1903 struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
1904
1905 /* Register some debug interfaces */
1906 xive_debugfs_init(xive);
1907}
1908
1909struct kvm_device_ops kvm_xive_ops = {
1910 .name = "kvm-xive",
1911 .create = kvmppc_xive_create,
1912 .init = kvmppc_xive_init,
1913 .destroy = kvmppc_xive_free,
1914 .set_attr = xive_set_attr,
1915 .get_attr = xive_get_attr,
1916 .has_attr = xive_has_attr,
1917};
1918
1919void kvmppc_xive_init_module(void)
1920{
1921 __xive_vm_h_xirr = xive_vm_h_xirr;
1922 __xive_vm_h_ipoll = xive_vm_h_ipoll;
1923 __xive_vm_h_ipi = xive_vm_h_ipi;
1924 __xive_vm_h_cppr = xive_vm_h_cppr;
1925 __xive_vm_h_eoi = xive_vm_h_eoi;
1926}
1927
1928void kvmppc_xive_exit_module(void)
1929{
1930 __xive_vm_h_xirr = NULL;
1931 __xive_vm_h_ipoll = NULL;
1932 __xive_vm_h_ipi = NULL;
1933 __xive_vm_h_cppr = NULL;
1934 __xive_vm_h_eoi = NULL;
1935}