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