blob: 741bf1f4387a5ed7b22a13a33ffa9fab70ace474 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002/*
3 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004 */
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>
Al Viro5bb866d2017-12-04 14:43:20 -050016#include <linux/uaccess.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100017#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>
Paolo Bonzini4415b332017-05-09 11:50:01 +020024#include <asm/debugfs.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100025#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 *
Finn Thain3cc97be2018-08-23 17:00:52 -070038 * We still instantiate them here because we use some of the
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100039 * 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))
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100048#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/*
Paul Mackerras95a64322018-10-08 16:30:55 +110062 * Push a vcpu's context to the XIVE on guest entry.
63 * This assumes we are in virtual mode (MMU on)
64 */
65void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu)
66{
67 void __iomem *tima = local_paca->kvm_hstate.xive_tima_virt;
68 u64 pq;
69
Paul Mackerras8d4ba9c2019-08-13 20:01:00 +100070 /*
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)
Paul Mackerras95a64322018-10-08 16:30:55 +110076 return;
Paul Mackerras8d4ba9c2019-08-13 20:01:00 +100077
Paul Mackerras95a64322018-10-08 16:30:55 +110078 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}
128EXPORT_SYMBOL_GPL(kvmppc_xive_push_vcpu);
129
130/*
Nicholas Piggin023c3c92021-05-28 19:07:28 +1000131 * Pull a vcpu's context from the XIVE on guest exit.
132 * This assumes we are in virtual mode (MMU on)
133 */
134void kvmppc_xive_pull_vcpu(struct kvm_vcpu *vcpu)
135{
136 void __iomem *tima = local_paca->kvm_hstate.xive_tima_virt;
137
138 if (!vcpu->arch.xive_pushed)
139 return;
140
141 /*
142 * Should not have been pushed if there is no tima
143 */
144 if (WARN_ON(!tima))
145 return;
146
147 eieio();
148 /* First load to pull the context, we ignore the value */
149 __raw_readl(tima + TM_SPC_PULL_OS_CTX);
150 /* Second load to recover the context state (Words 0 and 1) */
151 vcpu->arch.xive_saved_state.w01 = __raw_readq(tima + TM_QW1_OS);
152
153 /* Fixup some of the state for the next load */
154 vcpu->arch.xive_saved_state.lsmfb = 0;
155 vcpu->arch.xive_saved_state.ack = 0xff;
156 vcpu->arch.xive_pushed = 0;
157 eieio();
158}
159EXPORT_SYMBOL_GPL(kvmppc_xive_pull_vcpu);
160
161/*
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000162 * This is a simple trigger for a generic XIVE IRQ. This must
163 * only be called for interrupts that support a trigger page
164 */
165static bool xive_irq_trigger(struct xive_irq_data *xd)
166{
167 /* This should be only for MSIs */
168 if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
169 return false;
170
171 /* Those interrupts should always have a trigger page */
172 if (WARN_ON(!xd->trig_mmio))
173 return false;
174
175 out_be64(xd->trig_mmio, 0);
176
177 return true;
178}
179
180static irqreturn_t xive_esc_irq(int irq, void *data)
181{
182 struct kvm_vcpu *vcpu = data;
183
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +1100184 vcpu->arch.irq_pending = 1;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000185 smp_mb();
186 if (vcpu->arch.ceded)
187 kvmppc_fast_vcpu_kick(vcpu);
188
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100189 /* Since we have the no-EOI flag, the interrupt is effectively
190 * disabled now. Clearing xive_esc_on means we won't bother
191 * doing so on the next entry.
192 *
193 * This also allows the entry code to know that if a PQ combination
194 * of 10 is observed while xive_esc_on is true, it means the queue
195 * contains an unprocessed escalation interrupt. We don't make use of
196 * that knowledge today but might (see comment in book3s_hv_rmhandler.S)
197 */
198 vcpu->arch.xive_esc_on = false;
199
Paul Mackerrasda15c032019-08-13 20:06:48 +1000200 /* This orders xive_esc_on = false vs. subsequent stale_p = true */
201 smp_wmb(); /* goes with smp_mb() in cleanup_single_escalation */
202
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000203 return IRQ_HANDLED;
204}
205
Cédric Le Goater13ce3292019-04-18 12:39:31 +0200206int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio,
207 bool single_escalation)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000208{
209 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
210 struct xive_q *q = &xc->queues[prio];
211 char *name = NULL;
212 int rc;
213
214 /* Already there ? */
215 if (xc->esc_virq[prio])
216 return 0;
217
218 /* Hook up the escalation interrupt */
219 xc->esc_virq[prio] = irq_create_mapping(NULL, q->esc_irq);
220 if (!xc->esc_virq[prio]) {
221 pr_err("Failed to map escalation interrupt for queue %d of VCPU %d\n",
222 prio, xc->server_num);
223 return -EIO;
224 }
225
Cédric Le Goater13ce3292019-04-18 12:39:31 +0200226 if (single_escalation)
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100227 name = kasprintf(GFP_KERNEL, "kvm-%d-%d",
228 vcpu->kvm->arch.lpid, xc->server_num);
229 else
230 name = kasprintf(GFP_KERNEL, "kvm-%d-%d-%d",
231 vcpu->kvm->arch.lpid, xc->server_num, prio);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000232 if (!name) {
233 pr_err("Failed to allocate escalation irq name for queue %d of VCPU %d\n",
234 prio, xc->server_num);
235 rc = -ENOMEM;
236 goto error;
237 }
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100238
239 pr_devel("Escalation %s irq %d (prio %d)\n", name, xc->esc_virq[prio], prio);
240
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000241 rc = request_irq(xc->esc_virq[prio], xive_esc_irq,
242 IRQF_NO_THREAD, name, vcpu);
243 if (rc) {
244 pr_err("Failed to request escalation interrupt for queue %d of VCPU %d\n",
245 prio, xc->server_num);
246 goto error;
247 }
248 xc->esc_virq_names[prio] = name;
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100249
250 /* In single escalation mode, we grab the ESB MMIO of the
251 * interrupt and mask it. Also populate the VCPU v/raddr
252 * of the ESB page for use by asm entry/exit code. Finally
Cédric Le Goater4f1c3f72020-12-10 18:14:39 +0100253 * set the XIVE_IRQ_FLAG_NO_EOI flag which will prevent the
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100254 * core code from performing an EOI on the escalation
255 * interrupt, thus leaving it effectively masked after
256 * it fires once.
257 */
Cédric Le Goater13ce3292019-04-18 12:39:31 +0200258 if (single_escalation) {
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100259 struct irq_data *d = irq_get_irq_data(xc->esc_virq[prio]);
260 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
261
262 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
263 vcpu->arch.xive_esc_raddr = xd->eoi_page;
264 vcpu->arch.xive_esc_vaddr = (__force u64)xd->eoi_mmio;
Cédric Le Goater4f1c3f72020-12-10 18:14:39 +0100265 xd->flags |= XIVE_IRQ_FLAG_NO_EOI;
Benjamin Herrenschmidt9b9b13a2018-01-12 13:37:16 +1100266 }
267
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000268 return 0;
269error:
270 irq_dispose_mapping(xc->esc_virq[prio]);
271 xc->esc_virq[prio] = 0;
272 kfree(name);
273 return rc;
274}
275
276static int xive_provision_queue(struct kvm_vcpu *vcpu, u8 prio)
277{
278 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
279 struct kvmppc_xive *xive = xc->xive;
280 struct xive_q *q = &xc->queues[prio];
281 void *qpage;
282 int rc;
283
284 if (WARN_ON(q->qpage))
285 return 0;
286
287 /* Allocate the queue and retrieve infos on current node for now */
288 qpage = (__be32 *)__get_free_pages(GFP_KERNEL, xive->q_page_order);
289 if (!qpage) {
290 pr_err("Failed to allocate queue %d for VCPU %d\n",
291 prio, xc->server_num);
Ingo Molnared7158b2018-02-22 10:54:55 +0100292 return -ENOMEM;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000293 }
294 memset(qpage, 0, 1 << xive->q_order);
295
296 /*
297 * Reconfigure the queue. This will set q->qpage only once the
298 * queue is fully configured. This is a requirement for prio 0
299 * as we will stop doing EOIs for every IPI as soon as we observe
300 * qpage being non-NULL, and instead will only EOI when we receive
301 * corresponding queue 0 entries
302 */
303 rc = xive_native_configure_queue(xc->vp_id, q, prio, qpage,
304 xive->q_order, true);
305 if (rc)
306 pr_err("Failed to configure queue %d for VCPU %d\n",
307 prio, xc->server_num);
308 return rc;
309}
310
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +0200311/* Called with xive->lock held */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000312static int xive_check_provisioning(struct kvm *kvm, u8 prio)
313{
314 struct kvmppc_xive *xive = kvm->arch.xive;
315 struct kvm_vcpu *vcpu;
316 int i, rc;
317
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +0200318 lockdep_assert_held(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000319
320 /* Already provisioned ? */
321 if (xive->qmap & (1 << prio))
322 return 0;
323
324 pr_devel("Provisioning prio... %d\n", prio);
325
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100326 /* Provision each VCPU and enable escalations if needed */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000327 kvm_for_each_vcpu(i, vcpu, kvm) {
328 if (!vcpu->arch.xive_vcpu)
329 continue;
330 rc = xive_provision_queue(vcpu, prio);
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +1100331 if (rc == 0 && !xive->single_escalation)
Cédric Le Goater13ce3292019-04-18 12:39:31 +0200332 kvmppc_xive_attach_escalation(vcpu, prio,
333 xive->single_escalation);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000334 if (rc)
335 return rc;
336 }
337
338 /* Order previous stores and mark it as provisioned */
339 mb();
340 xive->qmap |= (1 << prio);
341 return 0;
342}
343
344static void xive_inc_q_pending(struct kvm *kvm, u32 server, u8 prio)
345{
346 struct kvm_vcpu *vcpu;
347 struct kvmppc_xive_vcpu *xc;
348 struct xive_q *q;
349
350 /* Locate target server */
351 vcpu = kvmppc_xive_find_server(kvm, server);
352 if (!vcpu) {
353 pr_warn("%s: Can't find server %d\n", __func__, server);
354 return;
355 }
356 xc = vcpu->arch.xive_vcpu;
357 if (WARN_ON(!xc))
358 return;
359
360 q = &xc->queues[prio];
361 atomic_inc(&q->pending_count);
362}
363
364static int xive_try_pick_queue(struct kvm_vcpu *vcpu, u8 prio)
365{
366 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
367 struct xive_q *q;
368 u32 max;
369
370 if (WARN_ON(!xc))
371 return -ENXIO;
372 if (!xc->valid)
373 return -ENXIO;
374
375 q = &xc->queues[prio];
376 if (WARN_ON(!q->qpage))
377 return -ENXIO;
378
379 /* Calculate max number of interrupts in that queue. */
380 max = (q->msk + 1) - XIVE_Q_GAP;
381 return atomic_add_unless(&q->count, 1, max) ? 0 : -EBUSY;
382}
383
Cédric Le Goatere8676ce2019-04-18 12:39:30 +0200384int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000385{
386 struct kvm_vcpu *vcpu;
387 int i, rc;
388
389 /* Locate target server */
390 vcpu = kvmppc_xive_find_server(kvm, *server);
391 if (!vcpu) {
392 pr_devel("Can't find server %d\n", *server);
393 return -EINVAL;
394 }
395
396 pr_devel("Finding irq target on 0x%x/%d...\n", *server, prio);
397
398 /* Try pick it */
399 rc = xive_try_pick_queue(vcpu, prio);
400 if (rc == 0)
401 return rc;
402
403 pr_devel(" .. failed, looking up candidate...\n");
404
405 /* Failed, pick another VCPU */
406 kvm_for_each_vcpu(i, vcpu, kvm) {
407 if (!vcpu->arch.xive_vcpu)
408 continue;
409 rc = xive_try_pick_queue(vcpu, prio);
410 if (rc == 0) {
411 *server = vcpu->arch.xive_vcpu->server_num;
412 pr_devel(" found on 0x%x/%d\n", *server, prio);
413 return rc;
414 }
415 }
416 pr_devel(" no available target !\n");
417
418 /* No available target ! */
419 return -EBUSY;
420}
421
422static u8 xive_lock_and_mask(struct kvmppc_xive *xive,
423 struct kvmppc_xive_src_block *sb,
424 struct kvmppc_xive_irq_state *state)
425{
426 struct xive_irq_data *xd;
427 u32 hw_num;
428 u8 old_prio;
429 u64 val;
430
431 /*
432 * Take the lock, set masked, try again if racing
433 * with H_EOI
434 */
435 for (;;) {
436 arch_spin_lock(&sb->lock);
437 old_prio = state->guest_priority;
438 state->guest_priority = MASKED;
439 mb();
440 if (!state->in_eoi)
441 break;
442 state->guest_priority = old_prio;
443 arch_spin_unlock(&sb->lock);
444 }
445
446 /* No change ? Bail */
447 if (old_prio == MASKED)
448 return old_prio;
449
450 /* Get the right irq */
451 kvmppc_xive_select_irq(state, &hw_num, &xd);
452
Cédric Le Goaterb5277d12020-12-10 18:14:46 +0100453 /* Set PQ to 10, return old P and old Q and remember them */
454 val = xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_10);
455 state->old_p = !!(val & 2);
456 state->old_q = !!(val & 1);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000457
Cédric Le Goaterb5277d12020-12-10 18:14:46 +0100458 /*
459 * Synchronize hardware to sensure the queues are updated when
460 * masking
461 */
462 xive_native_sync_source(hw_num);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000463
464 return old_prio;
465}
466
467static void xive_lock_for_unmask(struct kvmppc_xive_src_block *sb,
468 struct kvmppc_xive_irq_state *state)
469{
470 /*
471 * Take the lock try again if racing with H_EOI
472 */
473 for (;;) {
474 arch_spin_lock(&sb->lock);
475 if (!state->in_eoi)
476 break;
477 arch_spin_unlock(&sb->lock);
478 }
479}
480
481static void xive_finish_unmask(struct kvmppc_xive *xive,
482 struct kvmppc_xive_src_block *sb,
483 struct kvmppc_xive_irq_state *state,
484 u8 prio)
485{
486 struct xive_irq_data *xd;
487 u32 hw_num;
488
489 /* If we aren't changing a thing, move on */
490 if (state->guest_priority != MASKED)
491 goto bail;
492
493 /* Get the right irq */
494 kvmppc_xive_select_irq(state, &hw_num, &xd);
495
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000496 /* Old Q set, set PQ to 11 */
497 if (state->old_q)
498 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_11);
499
500 /*
501 * If not old P, then perform an "effective" EOI,
502 * on the source. This will handle the cases where
503 * FW EOI is needed.
504 */
505 if (!state->old_p)
506 xive_vm_source_eoi(hw_num, xd);
507
508 /* Synchronize ordering and mark unmasked */
509 mb();
510bail:
511 state->guest_priority = prio;
512}
513
514/*
515 * Target an interrupt to a given server/prio, this will fallback
516 * to another server if necessary and perform the HW targetting
517 * updates as needed
518 *
519 * NOTE: Must be called with the state lock held
520 */
521static int xive_target_interrupt(struct kvm *kvm,
522 struct kvmppc_xive_irq_state *state,
523 u32 server, u8 prio)
524{
525 struct kvmppc_xive *xive = kvm->arch.xive;
526 u32 hw_num;
527 int rc;
528
529 /*
530 * This will return a tentative server and actual
531 * priority. The count for that new target will have
532 * already been incremented.
533 */
Cédric Le Goatere8676ce2019-04-18 12:39:30 +0200534 rc = kvmppc_xive_select_target(kvm, &server, prio);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000535
536 /*
537 * We failed to find a target ? Not much we can do
538 * at least until we support the GIQ.
539 */
540 if (rc)
541 return rc;
542
543 /*
544 * Increment the old queue pending count if there
545 * was one so that the old queue count gets adjusted later
546 * when observed to be empty.
547 */
548 if (state->act_priority != MASKED)
549 xive_inc_q_pending(kvm,
550 state->act_server,
551 state->act_priority);
552 /*
553 * Update state and HW
554 */
555 state->act_priority = prio;
556 state->act_server = server;
557
558 /* Get the right irq */
559 kvmppc_xive_select_irq(state, &hw_num, NULL);
560
561 return xive_native_configure_irq(hw_num,
Cédric Le Goatereacc56b2019-04-18 12:39:28 +0200562 kvmppc_xive_vp(xive, server),
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000563 prio, state->number);
564}
565
566/*
567 * Targetting rules: In order to avoid losing track of
568 * pending interrupts accross mask and unmask, which would
569 * allow queue overflows, we implement the following rules:
570 *
571 * - Unless it was never enabled (or we run out of capacity)
572 * an interrupt is always targetted at a valid server/queue
573 * pair even when "masked" by the guest. This pair tends to
574 * be the last one used but it can be changed under some
575 * circumstances. That allows us to separate targetting
576 * from masking, we only handle accounting during (re)targetting,
577 * this also allows us to let an interrupt drain into its target
578 * queue after masking, avoiding complex schemes to remove
579 * interrupts out of remote processor queues.
580 *
581 * - When masking, we set PQ to 10 and save the previous value
582 * of P and Q.
583 *
584 * - When unmasking, if saved Q was set, we set PQ to 11
585 * otherwise we leave PQ to the HW state which will be either
586 * 10 if nothing happened or 11 if the interrupt fired while
587 * masked. Effectively we are OR'ing the previous Q into the
588 * HW Q.
589 *
590 * Then if saved P is clear, we do an effective EOI (Q->P->Trigger)
591 * which will unmask the interrupt and shoot a new one if Q was
592 * set.
593 *
594 * Otherwise (saved P is set) we leave PQ unchanged (so 10 or 11,
595 * effectively meaning an H_EOI from the guest is still expected
596 * for that interrupt).
597 *
598 * - If H_EOI occurs while masked, we clear the saved P.
599 *
600 * - When changing target, we account on the new target and
601 * increment a separate "pending" counter on the old one.
602 * This pending counter will be used to decrement the old
603 * target's count when its queue has been observed empty.
604 */
605
606int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
607 u32 priority)
608{
609 struct kvmppc_xive *xive = kvm->arch.xive;
610 struct kvmppc_xive_src_block *sb;
611 struct kvmppc_xive_irq_state *state;
612 u8 new_act_prio;
613 int rc = 0;
614 u16 idx;
615
616 if (!xive)
617 return -ENODEV;
618
619 pr_devel("set_xive ! irq 0x%x server 0x%x prio %d\n",
620 irq, server, priority);
621
622 /* First, check provisioning of queues */
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +0200623 if (priority != MASKED) {
624 mutex_lock(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000625 rc = xive_check_provisioning(xive->kvm,
626 xive_prio_from_guest(priority));
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +0200627 mutex_unlock(&xive->lock);
628 }
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000629 if (rc) {
630 pr_devel(" provisioning failure %d !\n", rc);
631 return rc;
632 }
633
634 sb = kvmppc_xive_find_source(xive, irq, &idx);
635 if (!sb)
636 return -EINVAL;
637 state = &sb->irq_state[idx];
638
639 /*
640 * We first handle masking/unmasking since the locking
641 * might need to be retried due to EOIs, we'll handle
642 * targetting changes later. These functions will return
643 * with the SB lock held.
644 *
645 * xive_lock_and_mask() will also set state->guest_priority
646 * but won't otherwise change other fields of the state.
647 *
648 * xive_lock_for_unmask will not actually unmask, this will
649 * be done later by xive_finish_unmask() once the targetting
650 * has been done, so we don't try to unmask an interrupt
651 * that hasn't yet been targetted.
652 */
653 if (priority == MASKED)
654 xive_lock_and_mask(xive, sb, state);
655 else
656 xive_lock_for_unmask(sb, state);
657
658
659 /*
660 * Then we handle targetting.
661 *
662 * First calculate a new "actual priority"
663 */
664 new_act_prio = state->act_priority;
665 if (priority != MASKED)
666 new_act_prio = xive_prio_from_guest(priority);
667
668 pr_devel(" new_act_prio=%x act_server=%x act_prio=%x\n",
669 new_act_prio, state->act_server, state->act_priority);
670
671 /*
672 * Then check if we actually need to change anything,
673 *
674 * The condition for re-targetting the interrupt is that
675 * we have a valid new priority (new_act_prio is not 0xff)
676 * and either the server or the priority changed.
677 *
678 * Note: If act_priority was ff and the new priority is
679 * also ff, we don't do anything and leave the interrupt
680 * untargetted. An attempt of doing an int_on on an
681 * untargetted interrupt will fail. If that is a problem
682 * we could initialize interrupts with valid default
683 */
684
685 if (new_act_prio != MASKED &&
686 (state->act_server != server ||
687 state->act_priority != new_act_prio))
688 rc = xive_target_interrupt(kvm, state, server, new_act_prio);
689
690 /*
691 * Perform the final unmasking of the interrupt source
692 * if necessary
693 */
694 if (priority != MASKED)
695 xive_finish_unmask(xive, sb, state, priority);
696
697 /*
698 * Finally Update saved_priority to match. Only int_on/off
699 * set this field to a different value.
700 */
701 state->saved_priority = priority;
702
703 arch_spin_unlock(&sb->lock);
704 return rc;
705}
706
707int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
708 u32 *priority)
709{
710 struct kvmppc_xive *xive = kvm->arch.xive;
711 struct kvmppc_xive_src_block *sb;
712 struct kvmppc_xive_irq_state *state;
713 u16 idx;
714
715 if (!xive)
716 return -ENODEV;
717
718 sb = kvmppc_xive_find_source(xive, irq, &idx);
719 if (!sb)
720 return -EINVAL;
721 state = &sb->irq_state[idx];
722 arch_spin_lock(&sb->lock);
Sam Bobroff2fb1e942017-09-26 16:47:04 +1000723 *server = state->act_server;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000724 *priority = state->guest_priority;
725 arch_spin_unlock(&sb->lock);
726
727 return 0;
728}
729
730int kvmppc_xive_int_on(struct kvm *kvm, u32 irq)
731{
732 struct kvmppc_xive *xive = kvm->arch.xive;
733 struct kvmppc_xive_src_block *sb;
734 struct kvmppc_xive_irq_state *state;
735 u16 idx;
736
737 if (!xive)
738 return -ENODEV;
739
740 sb = kvmppc_xive_find_source(xive, irq, &idx);
741 if (!sb)
742 return -EINVAL;
743 state = &sb->irq_state[idx];
744
745 pr_devel("int_on(irq=0x%x)\n", irq);
746
747 /*
748 * Check if interrupt was not targetted
749 */
750 if (state->act_priority == MASKED) {
751 pr_devel("int_on on untargetted interrupt\n");
752 return -EINVAL;
753 }
754
755 /* If saved_priority is 0xff, do nothing */
756 if (state->saved_priority == MASKED)
757 return 0;
758
759 /*
760 * Lock and unmask it.
761 */
762 xive_lock_for_unmask(sb, state);
763 xive_finish_unmask(xive, sb, state, state->saved_priority);
764 arch_spin_unlock(&sb->lock);
765
766 return 0;
767}
768
769int kvmppc_xive_int_off(struct kvm *kvm, u32 irq)
770{
771 struct kvmppc_xive *xive = kvm->arch.xive;
772 struct kvmppc_xive_src_block *sb;
773 struct kvmppc_xive_irq_state *state;
774 u16 idx;
775
776 if (!xive)
777 return -ENODEV;
778
779 sb = kvmppc_xive_find_source(xive, irq, &idx);
780 if (!sb)
781 return -EINVAL;
782 state = &sb->irq_state[idx];
783
784 pr_devel("int_off(irq=0x%x)\n", irq);
785
786 /*
787 * Lock and mask
788 */
789 state->saved_priority = xive_lock_and_mask(xive, sb, state);
790 arch_spin_unlock(&sb->lock);
791
792 return 0;
793}
794
795static bool xive_restore_pending_irq(struct kvmppc_xive *xive, u32 irq)
796{
797 struct kvmppc_xive_src_block *sb;
798 struct kvmppc_xive_irq_state *state;
799 u16 idx;
800
801 sb = kvmppc_xive_find_source(xive, irq, &idx);
802 if (!sb)
803 return false;
804 state = &sb->irq_state[idx];
805 if (!state->valid)
806 return false;
807
808 /*
809 * Trigger the IPI. This assumes we never restore a pass-through
810 * interrupt which should be safe enough
811 */
812 xive_irq_trigger(&state->ipi_data);
813
814 return true;
815}
816
817u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu)
818{
819 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
820
821 if (!xc)
822 return 0;
823
824 /* Return the per-cpu state for state saving/migration */
825 return (u64)xc->cppr << KVM_REG_PPC_ICP_CPPR_SHIFT |
Laurent Vivier7333b5a2017-12-12 18:23:56 +0100826 (u64)xc->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT |
827 (u64)0xff << KVM_REG_PPC_ICP_PPRI_SHIFT;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000828}
829
830int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
831{
832 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
833 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
834 u8 cppr, mfrr;
835 u32 xisr;
836
837 if (!xc || !xive)
838 return -ENOENT;
839
840 /* Grab individual state fields. We don't use pending_pri */
841 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
842 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
843 KVM_REG_PPC_ICP_XISR_MASK;
844 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
845
846 pr_devel("set_icp vcpu %d cppr=0x%x mfrr=0x%x xisr=0x%x\n",
847 xc->server_num, cppr, mfrr, xisr);
848
849 /*
850 * We can't update the state of a "pushed" VCPU, but that
Paul Mackerras6f868402019-04-29 11:24:03 +1000851 * shouldn't happen because the vcpu->mutex makes running a
852 * vcpu mutually exclusive with doing one_reg get/set on it.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000853 */
854 if (WARN_ON(vcpu->arch.xive_pushed))
855 return -EIO;
856
857 /* Update VCPU HW saved state */
858 vcpu->arch.xive_saved_state.cppr = cppr;
859 xc->hw_cppr = xc->cppr = cppr;
860
861 /*
862 * Update MFRR state. If it's not 0xff, we mark the VCPU as
863 * having a pending MFRR change, which will re-evaluate the
864 * target. The VCPU will thus potentially get a spurious
865 * interrupt but that's not a big deal.
866 */
867 xc->mfrr = mfrr;
868 if (mfrr < cppr)
869 xive_irq_trigger(&xc->vp_ipi_data);
870
871 /*
872 * Now saved XIRR is "interesting". It means there's something in
873 * the legacy "1 element" queue... for an IPI we simply ignore it,
874 * as the MFRR restore will handle that. For anything else we need
875 * to force a resend of the source.
876 * However the source may not have been setup yet. If that's the
877 * case, we keep that info and increment a counter in the xive to
878 * tell subsequent xive_set_source() to go look.
879 */
880 if (xisr > XICS_IPI && !xive_restore_pending_irq(xive, xisr)) {
881 xc->delayed_irq = xisr;
882 xive->delayed_irqs++;
883 pr_devel(" xisr restore delayed\n");
884 }
885
886 return 0;
887}
888
889int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
890 struct irq_desc *host_desc)
891{
892 struct kvmppc_xive *xive = kvm->arch.xive;
893 struct kvmppc_xive_src_block *sb;
894 struct kvmppc_xive_irq_state *state;
895 struct irq_data *host_data = irq_desc_get_irq_data(host_desc);
896 unsigned int host_irq = irq_desc_get_irq(host_desc);
897 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(host_data);
898 u16 idx;
899 u8 prio;
900 int rc;
901
902 if (!xive)
903 return -ENODEV;
904
905 pr_devel("set_mapped girq 0x%lx host HW irq 0x%x...\n",guest_irq, hw_irq);
906
907 sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
908 if (!sb)
909 return -EINVAL;
910 state = &sb->irq_state[idx];
911
912 /*
913 * Mark the passed-through interrupt as going to a VCPU,
914 * this will prevent further EOIs and similar operations
915 * from the XIVE code. It will also mask the interrupt
916 * to either PQ=10 or 11 state, the latter if the interrupt
917 * is pending. This will allow us to unmask or retrigger it
918 * after routing it to the guest with a simple EOI.
919 *
920 * The "state" argument is a "token", all it needs is to be
921 * non-NULL to switch to passed-through or NULL for the
922 * other way around. We may not yet have an actual VCPU
923 * target here and we don't really care.
924 */
925 rc = irq_set_vcpu_affinity(host_irq, state);
926 if (rc) {
927 pr_err("Failed to set VCPU affinity for irq %d\n", host_irq);
928 return rc;
929 }
930
931 /*
932 * Mask and read state of IPI. We need to know if its P bit
933 * is set as that means it's potentially already using a
934 * queue entry in the target
935 */
936 prio = xive_lock_and_mask(xive, sb, state);
937 pr_devel(" old IPI prio %02x P:%d Q:%d\n", prio,
938 state->old_p, state->old_q);
939
940 /* Turn the IPI hard off */
941 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
942
Cédric Le Goater232b9842019-04-18 12:39:39 +0200943 /*
944 * Reset ESB guest mapping. Needed when ESB pages are exposed
945 * to the guest in XIVE native mode
946 */
947 if (xive->ops && xive->ops->reset_mapped)
948 xive->ops->reset_mapped(kvm, guest_irq);
949
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000950 /* Grab info about irq */
951 state->pt_number = hw_irq;
952 state->pt_data = irq_data_get_irq_handler_data(host_data);
953
954 /*
955 * Configure the IRQ to match the existing configuration of
956 * the IPI if it was already targetted. Otherwise this will
957 * mask the interrupt in a lossy way (act_priority is 0xff)
958 * which is fine for a never started interrupt.
959 */
960 xive_native_configure_irq(hw_irq,
Cédric Le Goatereacc56b2019-04-18 12:39:28 +0200961 kvmppc_xive_vp(xive, state->act_server),
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000962 state->act_priority, state->number);
963
964 /*
965 * We do an EOI to enable the interrupt (and retrigger if needed)
966 * if the guest has the interrupt unmasked and the P bit was *not*
967 * set in the IPI. If it was set, we know a slot may still be in
968 * use in the target queue thus we have to wait for a guest
969 * originated EOI
970 */
971 if (prio != MASKED && !state->old_p)
972 xive_vm_source_eoi(hw_irq, state->pt_data);
973
974 /* Clear old_p/old_q as they are no longer relevant */
975 state->old_p = state->old_q = false;
976
977 /* Restore guest prio (unlocks EOI) */
978 mb();
979 state->guest_priority = prio;
980 arch_spin_unlock(&sb->lock);
981
982 return 0;
983}
984EXPORT_SYMBOL_GPL(kvmppc_xive_set_mapped);
985
986int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
987 struct irq_desc *host_desc)
988{
989 struct kvmppc_xive *xive = kvm->arch.xive;
990 struct kvmppc_xive_src_block *sb;
991 struct kvmppc_xive_irq_state *state;
992 unsigned int host_irq = irq_desc_get_irq(host_desc);
993 u16 idx;
994 u8 prio;
995 int rc;
996
997 if (!xive)
998 return -ENODEV;
999
1000 pr_devel("clr_mapped girq 0x%lx...\n", guest_irq);
1001
1002 sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
1003 if (!sb)
1004 return -EINVAL;
1005 state = &sb->irq_state[idx];
1006
1007 /*
1008 * Mask and read state of IRQ. We need to know if its P bit
1009 * is set as that means it's potentially already using a
1010 * queue entry in the target
1011 */
1012 prio = xive_lock_and_mask(xive, sb, state);
1013 pr_devel(" old IRQ prio %02x P:%d Q:%d\n", prio,
1014 state->old_p, state->old_q);
1015
1016 /*
1017 * If old_p is set, the interrupt is pending, we switch it to
1018 * PQ=11. This will force a resend in the host so the interrupt
1019 * isn't lost to whatver host driver may pick it up
1020 */
1021 if (state->old_p)
1022 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_11);
1023
1024 /* Release the passed-through interrupt to the host */
1025 rc = irq_set_vcpu_affinity(host_irq, NULL);
1026 if (rc) {
1027 pr_err("Failed to clr VCPU affinity for irq %d\n", host_irq);
1028 return rc;
1029 }
1030
1031 /* Forget about the IRQ */
1032 state->pt_number = 0;
1033 state->pt_data = NULL;
1034
Cédric Le Goater232b9842019-04-18 12:39:39 +02001035 /*
1036 * Reset ESB guest mapping. Needed when ESB pages are exposed
1037 * to the guest in XIVE native mode
1038 */
1039 if (xive->ops && xive->ops->reset_mapped) {
1040 xive->ops->reset_mapped(kvm, guest_irq);
1041 }
1042
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001043 /* Reconfigure the IPI */
1044 xive_native_configure_irq(state->ipi_number,
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02001045 kvmppc_xive_vp(xive, state->act_server),
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001046 state->act_priority, state->number);
1047
1048 /*
1049 * If old_p is set (we have a queue entry potentially
1050 * occupied) or the interrupt is masked, we set the IPI
1051 * to PQ=10 state. Otherwise we just re-enable it (PQ=00).
1052 */
1053 if (prio == MASKED || state->old_p)
1054 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_10);
1055 else
1056 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_00);
1057
1058 /* Restore guest prio (unlocks EOI) */
1059 mb();
1060 state->guest_priority = prio;
1061 arch_spin_unlock(&sb->lock);
1062
1063 return 0;
1064}
1065EXPORT_SYMBOL_GPL(kvmppc_xive_clr_mapped);
1066
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02001067void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001068{
1069 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1070 struct kvm *kvm = vcpu->kvm;
1071 struct kvmppc_xive *xive = kvm->arch.xive;
1072 int i, j;
1073
1074 for (i = 0; i <= xive->max_sbid; i++) {
1075 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1076
1077 if (!sb)
1078 continue;
1079 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
1080 struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
1081
1082 if (!state->valid)
1083 continue;
1084 if (state->act_priority == MASKED)
1085 continue;
1086 if (state->act_server != xc->server_num)
1087 continue;
1088
1089 /* Clean it up */
1090 arch_spin_lock(&sb->lock);
1091 state->act_priority = MASKED;
1092 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
1093 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
1094 if (state->pt_number) {
1095 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
1096 xive_native_configure_irq(state->pt_number, 0, MASKED, 0);
1097 }
1098 arch_spin_unlock(&sb->lock);
1099 }
1100 }
Paul Mackerras0caecf52019-04-26 16:54:14 +10001101
1102 /* Disable vcpu's escalation interrupt */
1103 if (vcpu->arch.xive_esc_on) {
1104 __raw_readq((void __iomem *)(vcpu->arch.xive_esc_vaddr +
1105 XIVE_ESB_SET_PQ_01));
1106 vcpu->arch.xive_esc_on = false;
1107 }
1108
1109 /*
1110 * Clear pointers to escalation interrupt ESB.
1111 * This is safe because the vcpu->mutex is held, preventing
1112 * any other CPU from concurrently executing a KVM_RUN ioctl.
1113 */
1114 vcpu->arch.xive_esc_vaddr = 0;
1115 vcpu->arch.xive_esc_raddr = 0;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001116}
1117
Paul Mackerrasda15c032019-08-13 20:06:48 +10001118/*
1119 * In single escalation mode, the escalation interrupt is marked so
1120 * that EOI doesn't re-enable it, but just sets the stale_p flag to
1121 * indicate that the P bit has already been dealt with. However, the
1122 * assembly code that enters the guest sets PQ to 00 without clearing
1123 * stale_p (because it has no easy way to address it). Hence we have
1124 * to adjust stale_p before shutting down the interrupt.
1125 */
1126void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
1127 struct kvmppc_xive_vcpu *xc, int irq)
1128{
1129 struct irq_data *d = irq_get_irq_data(irq);
1130 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
1131
1132 /*
1133 * This slightly odd sequence gives the right result
1134 * (i.e. stale_p set if xive_esc_on is false) even if
1135 * we race with xive_esc_irq() and xive_irq_eoi().
1136 */
1137 xd->stale_p = false;
1138 smp_mb(); /* paired with smb_wmb in xive_esc_irq */
1139 if (!vcpu->arch.xive_esc_on)
1140 xd->stale_p = true;
1141}
1142
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001143void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
1144{
1145 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
Cédric Le Goater5422e952019-04-18 12:39:42 +02001146 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001147 int i;
1148
Cédric Le Goater5422e952019-04-18 12:39:42 +02001149 if (!kvmppc_xics_enabled(vcpu))
1150 return;
1151
1152 if (!xc)
1153 return;
1154
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001155 pr_devel("cleanup_vcpu(cpu=%d)\n", xc->server_num);
1156
1157 /* Ensure no interrupt is still routed to that VP */
1158 xc->valid = false;
1159 kvmppc_xive_disable_vcpu_interrupts(vcpu);
1160
1161 /* Mask the VP IPI */
1162 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01);
1163
Cédric Le Goater237aed42019-08-06 19:25:38 +02001164 /* Free escalations */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001165 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001166 if (xc->esc_virq[i]) {
Paul Mackerrasda15c032019-08-13 20:06:48 +10001167 if (xc->xive->single_escalation)
1168 xive_cleanup_single_escalation(vcpu, xc,
1169 xc->esc_virq[i]);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001170 free_irq(xc->esc_virq[i], vcpu);
1171 irq_dispose_mapping(xc->esc_virq[i]);
1172 kfree(xc->esc_virq_names[i]);
1173 }
Cédric Le Goater237aed42019-08-06 19:25:38 +02001174 }
1175
1176 /* Disable the VP */
1177 xive_native_disable_vp(xc->vp_id);
1178
Paul Mackerras8d4ba9c2019-08-13 20:01:00 +10001179 /* Clear the cam word so guest entry won't try to push context */
1180 vcpu->arch.xive_cam_word = 0;
1181
Cédric Le Goater237aed42019-08-06 19:25:38 +02001182 /* Free the queues */
1183 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1184 struct xive_q *q = &xc->queues[i];
1185
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001186 xive_native_disable_queue(xc->vp_id, q, i);
1187 if (q->qpage) {
1188 free_pages((unsigned long)q->qpage,
1189 xive->q_page_order);
1190 q->qpage = NULL;
1191 }
1192 }
1193
1194 /* Free the IPI */
1195 if (xc->vp_ipi) {
1196 xive_cleanup_irq_data(&xc->vp_ipi_data);
1197 xive_native_free_irq(xc->vp_ipi);
1198 }
1199 /* Free the VP */
1200 kfree(xc);
Cédric Le Goater5422e952019-04-18 12:39:42 +02001201
1202 /* Cleanup the vcpu */
1203 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
1204 vcpu->arch.xive_vcpu = NULL;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001205}
1206
Greg Kurz8db29ea2019-09-27 13:53:55 +02001207static bool kvmppc_xive_vcpu_id_valid(struct kvmppc_xive *xive, u32 cpu)
1208{
Greg Kurz062cfab2019-09-27 13:54:01 +02001209 /* We have a block of xive->nr_servers VPs. We just need to check
Greg Kurzf54db392020-11-30 13:19:27 +01001210 * packed vCPU ids are below that.
Greg Kurz8db29ea2019-09-27 13:53:55 +02001211 */
Greg Kurzf54db392020-11-30 13:19:27 +01001212 return kvmppc_pack_vcpu_id(xive->kvm, cpu) < xive->nr_servers;
Greg Kurz8db29ea2019-09-27 13:53:55 +02001213}
1214
1215int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp)
1216{
1217 u32 vp_id;
1218
1219 if (!kvmppc_xive_vcpu_id_valid(xive, cpu)) {
1220 pr_devel("Out of bounds !\n");
1221 return -EINVAL;
1222 }
1223
Greg Kurz062cfab2019-09-27 13:54:01 +02001224 if (xive->vp_base == XIVE_INVALID_VP) {
1225 xive->vp_base = xive_native_alloc_vp_block(xive->nr_servers);
1226 pr_devel("VP_Base=%x nr_servers=%d\n", xive->vp_base, xive->nr_servers);
1227
1228 if (xive->vp_base == XIVE_INVALID_VP)
1229 return -ENOSPC;
1230 }
1231
Greg Kurz8db29ea2019-09-27 13:53:55 +02001232 vp_id = kvmppc_xive_vp(xive, cpu);
1233 if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
1234 pr_devel("Duplicate !\n");
1235 return -EEXIST;
1236 }
1237
1238 *vp = vp_id;
1239
1240 return 0;
1241}
1242
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001243int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
1244 struct kvm_vcpu *vcpu, u32 cpu)
1245{
1246 struct kvmppc_xive *xive = dev->private;
1247 struct kvmppc_xive_vcpu *xc;
1248 int i, r = -EBUSY;
Greg Kurz12ade69c2019-09-27 13:53:43 +02001249 u32 vp_id;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001250
1251 pr_devel("connect_vcpu(cpu=%d)\n", cpu);
1252
1253 if (dev->ops != &kvm_xive_ops) {
1254 pr_devel("Wrong ops !\n");
1255 return -EPERM;
1256 }
1257 if (xive->kvm != vcpu->kvm)
1258 return -EPERM;
Cédric Le Goater5422e952019-04-18 12:39:42 +02001259 if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001260 return -EBUSY;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001261
1262 /* We need to synchronize with queue provisioning */
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02001263 mutex_lock(&xive->lock);
Greg Kurz12ade69c2019-09-27 13:53:43 +02001264
Greg Kurz8db29ea2019-09-27 13:53:55 +02001265 r = kvmppc_xive_compute_vp_id(xive, cpu, &vp_id);
1266 if (r)
Greg Kurz12ade69c2019-09-27 13:53:43 +02001267 goto bail;
Greg Kurz12ade69c2019-09-27 13:53:43 +02001268
1269 xc = kzalloc(sizeof(*xc), GFP_KERNEL);
1270 if (!xc) {
1271 r = -ENOMEM;
1272 goto bail;
1273 }
1274
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001275 vcpu->arch.xive_vcpu = xc;
1276 xc->xive = xive;
1277 xc->vcpu = vcpu;
1278 xc->server_num = cpu;
Greg Kurz12ade69c2019-09-27 13:53:43 +02001279 xc->vp_id = vp_id;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001280 xc->mfrr = 0xff;
1281 xc->valid = true;
1282
1283 r = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
1284 if (r)
1285 goto bail;
1286
1287 /* Configure VCPU fields for use by assembly push/pull */
1288 vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
1289 vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
1290
1291 /* Allocate IPI */
1292 xc->vp_ipi = xive_native_alloc_irq();
1293 if (!xc->vp_ipi) {
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001294 pr_err("Failed to allocate xive irq for VCPU IPI\n");
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001295 r = -EIO;
1296 goto bail;
1297 }
1298 pr_devel(" IPI=0x%x\n", xc->vp_ipi);
1299
1300 r = xive_native_populate_irq_data(xc->vp_ipi, &xc->vp_ipi_data);
1301 if (r)
1302 goto bail;
1303
1304 /*
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001305 * Enable the VP first as the single escalation mode will
1306 * affect escalation interrupts numbering
1307 */
1308 r = xive_native_enable_vp(xc->vp_id, xive->single_escalation);
1309 if (r) {
1310 pr_err("Failed to enable VP in OPAL, err %d\n", r);
1311 goto bail;
1312 }
1313
1314 /*
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001315 * Initialize queues. Initially we set them all for no queueing
1316 * and we enable escalation for queue 0 only which we'll use for
1317 * our mfrr change notifications. If the VCPU is hot-plugged, we
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001318 * do handle provisioning however based on the existing "map"
1319 * of enabled queues.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001320 */
1321 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
1322 struct xive_q *q = &xc->queues[i];
1323
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001324 /* Single escalation, no queue 7 */
1325 if (i == 7 && xive->single_escalation)
1326 break;
1327
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001328 /* Is queue already enabled ? Provision it */
1329 if (xive->qmap & (1 << i)) {
1330 r = xive_provision_queue(vcpu, i);
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001331 if (r == 0 && !xive->single_escalation)
Cédric Le Goater13ce3292019-04-18 12:39:31 +02001332 kvmppc_xive_attach_escalation(
1333 vcpu, i, xive->single_escalation);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001334 if (r)
1335 goto bail;
1336 } else {
1337 r = xive_native_configure_queue(xc->vp_id,
1338 q, i, NULL, 0, true);
1339 if (r) {
1340 pr_err("Failed to configure queue %d for VCPU %d\n",
1341 i, cpu);
1342 goto bail;
1343 }
1344 }
1345 }
1346
1347 /* If not done above, attach priority 0 escalation */
Cédric Le Goater13ce3292019-04-18 12:39:31 +02001348 r = kvmppc_xive_attach_escalation(vcpu, 0, xive->single_escalation);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001349 if (r)
1350 goto bail;
1351
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001352 /* Route the IPI */
1353 r = xive_native_configure_irq(xc->vp_ipi, xc->vp_id, 0, XICS_IPI);
1354 if (!r)
1355 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_00);
1356
1357bail:
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02001358 mutex_unlock(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001359 if (r) {
1360 kvmppc_xive_cleanup_vcpu(vcpu);
1361 return r;
1362 }
1363
1364 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1365 return 0;
1366}
1367
1368/*
1369 * Scanning of queues before/after migration save
1370 */
1371static void xive_pre_save_set_queued(struct kvmppc_xive *xive, u32 irq)
1372{
1373 struct kvmppc_xive_src_block *sb;
1374 struct kvmppc_xive_irq_state *state;
1375 u16 idx;
1376
1377 sb = kvmppc_xive_find_source(xive, irq, &idx);
1378 if (!sb)
1379 return;
1380
1381 state = &sb->irq_state[idx];
1382
1383 /* Some sanity checking */
1384 if (!state->valid) {
1385 pr_err("invalid irq 0x%x in cpu queue!\n", irq);
1386 return;
1387 }
1388
1389 /*
1390 * If the interrupt is in a queue it should have P set.
1391 * We warn so that gets reported. A backtrace isn't useful
1392 * so no need to use a WARN_ON.
1393 */
1394 if (!state->saved_p)
1395 pr_err("Interrupt 0x%x is marked in a queue but P not set !\n", irq);
1396
1397 /* Set flag */
1398 state->in_queue = true;
1399}
1400
1401static void xive_pre_save_mask_irq(struct kvmppc_xive *xive,
1402 struct kvmppc_xive_src_block *sb,
1403 u32 irq)
1404{
1405 struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
1406
1407 if (!state->valid)
1408 return;
1409
1410 /* Mask and save state, this will also sync HW queues */
1411 state->saved_scan_prio = xive_lock_and_mask(xive, sb, state);
1412
1413 /* Transfer P and Q */
1414 state->saved_p = state->old_p;
1415 state->saved_q = state->old_q;
1416
1417 /* Unlock */
1418 arch_spin_unlock(&sb->lock);
1419}
1420
1421static void xive_pre_save_unmask_irq(struct kvmppc_xive *xive,
1422 struct kvmppc_xive_src_block *sb,
1423 u32 irq)
1424{
1425 struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
1426
1427 if (!state->valid)
1428 return;
1429
1430 /*
1431 * Lock / exclude EOI (not technically necessary if the
1432 * guest isn't running concurrently. If this becomes a
1433 * performance issue we can probably remove the lock.
1434 */
1435 xive_lock_for_unmask(sb, state);
1436
1437 /* Restore mask/prio if it wasn't masked */
1438 if (state->saved_scan_prio != MASKED)
1439 xive_finish_unmask(xive, sb, state, state->saved_scan_prio);
1440
1441 /* Unlock */
1442 arch_spin_unlock(&sb->lock);
1443}
1444
1445static void xive_pre_save_queue(struct kvmppc_xive *xive, struct xive_q *q)
1446{
1447 u32 idx = q->idx;
1448 u32 toggle = q->toggle;
1449 u32 irq;
1450
1451 do {
1452 irq = __xive_read_eq(q->qpage, q->msk, &idx, &toggle);
1453 if (irq > XICS_IPI)
1454 xive_pre_save_set_queued(xive, irq);
1455 } while(irq);
1456}
1457
1458static void xive_pre_save_scan(struct kvmppc_xive *xive)
1459{
1460 struct kvm_vcpu *vcpu = NULL;
1461 int i, j;
1462
1463 /*
1464 * See comment in xive_get_source() about how this
1465 * work. Collect a stable state for all interrupts
1466 */
1467 for (i = 0; i <= xive->max_sbid; i++) {
1468 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1469 if (!sb)
1470 continue;
1471 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
1472 xive_pre_save_mask_irq(xive, sb, j);
1473 }
1474
1475 /* Then scan the queues and update the "in_queue" flag */
1476 kvm_for_each_vcpu(i, vcpu, xive->kvm) {
1477 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1478 if (!xc)
1479 continue;
1480 for (j = 0; j < KVMPPC_XIVE_Q_COUNT; j++) {
Paul Mackerras00c14752017-06-30 16:39:55 +10001481 if (xc->queues[j].qpage)
1482 xive_pre_save_queue(xive, &xc->queues[j]);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001483 }
1484 }
1485
1486 /* Finally restore interrupt states */
1487 for (i = 0; i <= xive->max_sbid; i++) {
1488 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1489 if (!sb)
1490 continue;
1491 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
1492 xive_pre_save_unmask_irq(xive, sb, j);
1493 }
1494}
1495
1496static void xive_post_save_scan(struct kvmppc_xive *xive)
1497{
1498 u32 i, j;
1499
1500 /* Clear all the in_queue flags */
1501 for (i = 0; i <= xive->max_sbid; i++) {
1502 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1503 if (!sb)
1504 continue;
1505 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
1506 sb->irq_state[j].in_queue = false;
1507 }
1508
1509 /* Next get_source() will do a new scan */
1510 xive->saved_src_count = 0;
1511}
1512
1513/*
1514 * This returns the source configuration and state to user space.
1515 */
1516static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr)
1517{
1518 struct kvmppc_xive_src_block *sb;
1519 struct kvmppc_xive_irq_state *state;
1520 u64 __user *ubufp = (u64 __user *) addr;
1521 u64 val, prio;
1522 u16 idx;
1523
1524 sb = kvmppc_xive_find_source(xive, irq, &idx);
1525 if (!sb)
1526 return -ENOENT;
1527
1528 state = &sb->irq_state[idx];
1529
1530 if (!state->valid)
1531 return -ENOENT;
1532
1533 pr_devel("get_source(%ld)...\n", irq);
1534
1535 /*
1536 * So to properly save the state into something that looks like a
1537 * XICS migration stream we cannot treat interrupts individually.
1538 *
1539 * We need, instead, mask them all (& save their previous PQ state)
1540 * to get a stable state in the HW, then sync them to ensure that
1541 * any interrupt that had already fired hits its queue, and finally
1542 * scan all the queues to collect which interrupts are still present
1543 * in the queues, so we can set the "pending" flag on them and
1544 * they can be resent on restore.
1545 *
1546 * So we do it all when the "first" interrupt gets saved, all the
1547 * state is collected at that point, the rest of xive_get_source()
1548 * will merely collect and convert that state to the expected
1549 * userspace bit mask.
1550 */
1551 if (xive->saved_src_count == 0)
1552 xive_pre_save_scan(xive);
1553 xive->saved_src_count++;
1554
1555 /* Convert saved state into something compatible with xics */
Sam Bobroff2fb1e942017-09-26 16:47:04 +10001556 val = state->act_server;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001557 prio = state->saved_scan_prio;
1558
1559 if (prio == MASKED) {
1560 val |= KVM_XICS_MASKED;
1561 prio = state->saved_priority;
1562 }
1563 val |= prio << KVM_XICS_PRIORITY_SHIFT;
1564 if (state->lsi) {
1565 val |= KVM_XICS_LEVEL_SENSITIVE;
1566 if (state->saved_p)
1567 val |= KVM_XICS_PENDING;
1568 } else {
1569 if (state->saved_p)
1570 val |= KVM_XICS_PRESENTED;
1571
1572 if (state->saved_q)
1573 val |= KVM_XICS_QUEUED;
1574
1575 /*
1576 * We mark it pending (which will attempt a re-delivery)
1577 * if we are in a queue *or* we were masked and had
1578 * Q set which is equivalent to the XICS "masked pending"
1579 * state
1580 */
1581 if (state->in_queue || (prio == MASKED && state->saved_q))
1582 val |= KVM_XICS_PENDING;
1583 }
1584
1585 /*
1586 * If that was the last interrupt saved, reset the
1587 * in_queue flags
1588 */
1589 if (xive->saved_src_count == xive->src_count)
1590 xive_post_save_scan(xive);
1591
1592 /* Copy the result to userspace */
1593 if (put_user(val, ubufp))
1594 return -EFAULT;
1595
1596 return 0;
1597}
1598
Cédric Le Goater4131f832019-04-18 12:39:29 +02001599struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(
1600 struct kvmppc_xive *xive, int irq)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001601{
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001602 struct kvmppc_xive_src_block *sb;
1603 int i, bid;
1604
1605 bid = irq >> KVMPPC_XICS_ICS_SHIFT;
1606
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02001607 mutex_lock(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001608
1609 /* block already exists - somebody else got here first */
1610 if (xive->src_blocks[bid])
1611 goto out;
1612
1613 /* Create the ICS */
1614 sb = kzalloc(sizeof(*sb), GFP_KERNEL);
1615 if (!sb)
1616 goto out;
1617
1618 sb->id = bid;
1619
1620 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1621 sb->irq_state[i].number = (bid << KVMPPC_XICS_ICS_SHIFT) | i;
Cédric Le Goatere8676ce2019-04-18 12:39:30 +02001622 sb->irq_state[i].eisn = 0;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001623 sb->irq_state[i].guest_priority = MASKED;
1624 sb->irq_state[i].saved_priority = MASKED;
1625 sb->irq_state[i].act_priority = MASKED;
1626 }
1627 smp_wmb();
1628 xive->src_blocks[bid] = sb;
1629
1630 if (bid > xive->max_sbid)
1631 xive->max_sbid = bid;
1632
1633out:
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02001634 mutex_unlock(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001635 return xive->src_blocks[bid];
1636}
1637
1638static bool xive_check_delayed_irq(struct kvmppc_xive *xive, u32 irq)
1639{
1640 struct kvm *kvm = xive->kvm;
1641 struct kvm_vcpu *vcpu = NULL;
1642 int i;
1643
1644 kvm_for_each_vcpu(i, vcpu, kvm) {
1645 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1646
1647 if (!xc)
1648 continue;
1649
1650 if (xc->delayed_irq == irq) {
1651 xc->delayed_irq = 0;
1652 xive->delayed_irqs--;
1653 return true;
1654 }
1655 }
1656 return false;
1657}
1658
1659static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr)
1660{
1661 struct kvmppc_xive_src_block *sb;
1662 struct kvmppc_xive_irq_state *state;
1663 u64 __user *ubufp = (u64 __user *) addr;
1664 u16 idx;
1665 u64 val;
1666 u8 act_prio, guest_prio;
1667 u32 server;
1668 int rc = 0;
1669
1670 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1671 return -ENOENT;
1672
1673 pr_devel("set_source(irq=0x%lx)\n", irq);
1674
1675 /* Find the source */
1676 sb = kvmppc_xive_find_source(xive, irq, &idx);
1677 if (!sb) {
1678 pr_devel("No source, creating source block...\n");
Cédric Le Goater4131f832019-04-18 12:39:29 +02001679 sb = kvmppc_xive_create_src_block(xive, irq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001680 if (!sb) {
1681 pr_devel("Failed to create block...\n");
1682 return -ENOMEM;
1683 }
1684 }
1685 state = &sb->irq_state[idx];
1686
1687 /* Read user passed data */
1688 if (get_user(val, ubufp)) {
1689 pr_devel("fault getting user info !\n");
1690 return -EFAULT;
1691 }
1692
1693 server = val & KVM_XICS_DESTINATION_MASK;
1694 guest_prio = val >> KVM_XICS_PRIORITY_SHIFT;
1695
1696 pr_devel(" val=0x016%llx (server=0x%x, guest_prio=%d)\n",
1697 val, server, guest_prio);
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11001698
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001699 /*
1700 * If the source doesn't already have an IPI, allocate
1701 * one and get the corresponding data
1702 */
1703 if (!state->ipi_number) {
1704 state->ipi_number = xive_native_alloc_irq();
1705 if (state->ipi_number == 0) {
1706 pr_devel("Failed to allocate IPI !\n");
1707 return -ENOMEM;
1708 }
1709 xive_native_populate_irq_data(state->ipi_number, &state->ipi_data);
1710 pr_devel(" src_ipi=0x%x\n", state->ipi_number);
1711 }
1712
1713 /*
1714 * We use lock_and_mask() to set us in the right masked
1715 * state. We will override that state from the saved state
1716 * further down, but this will handle the cases of interrupts
1717 * that need FW masking. We set the initial guest_priority to
1718 * 0 before calling it to ensure it actually performs the masking.
1719 */
1720 state->guest_priority = 0;
1721 xive_lock_and_mask(xive, sb, state);
1722
1723 /*
1724 * Now, we select a target if we have one. If we don't we
1725 * leave the interrupt untargetted. It means that an interrupt
1726 * can become "untargetted" accross migration if it was masked
1727 * by set_xive() but there is little we can do about it.
1728 */
1729
1730 /* First convert prio and mark interrupt as untargetted */
1731 act_prio = xive_prio_from_guest(guest_prio);
1732 state->act_priority = MASKED;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001733
1734 /*
1735 * We need to drop the lock due to the mutex below. Hopefully
1736 * nothing is touching that interrupt yet since it hasn't been
1737 * advertized to a running guest yet
1738 */
1739 arch_spin_unlock(&sb->lock);
1740
1741 /* If we have a priority target the interrupt */
1742 if (act_prio != MASKED) {
1743 /* First, check provisioning of queues */
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02001744 mutex_lock(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001745 rc = xive_check_provisioning(xive->kvm, act_prio);
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02001746 mutex_unlock(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001747
1748 /* Target interrupt */
1749 if (rc == 0)
1750 rc = xive_target_interrupt(xive->kvm, state,
1751 server, act_prio);
1752 /*
1753 * If provisioning or targetting failed, leave it
1754 * alone and masked. It will remain disabled until
1755 * the guest re-targets it.
1756 */
1757 }
1758
1759 /*
1760 * Find out if this was a delayed irq stashed in an ICP,
1761 * in which case, treat it as pending
1762 */
1763 if (xive->delayed_irqs && xive_check_delayed_irq(xive, irq)) {
1764 val |= KVM_XICS_PENDING;
1765 pr_devel(" Found delayed ! forcing PENDING !\n");
1766 }
1767
1768 /* Cleanup the SW state */
1769 state->old_p = false;
1770 state->old_q = false;
1771 state->lsi = false;
1772 state->asserted = false;
1773
1774 /* Restore LSI state */
1775 if (val & KVM_XICS_LEVEL_SENSITIVE) {
1776 state->lsi = true;
1777 if (val & KVM_XICS_PENDING)
1778 state->asserted = true;
1779 pr_devel(" LSI ! Asserted=%d\n", state->asserted);
1780 }
1781
1782 /*
1783 * Restore P and Q. If the interrupt was pending, we
Cédric Le Goaterdc1c4162017-12-12 12:02:04 +00001784 * force Q and !P, which will trigger a resend.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001785 *
1786 * That means that a guest that had both an interrupt
1787 * pending (queued) and Q set will restore with only
1788 * one instance of that interrupt instead of 2, but that
1789 * is perfectly fine as coalescing interrupts that haven't
1790 * been presented yet is always allowed.
1791 */
Cédric Le Goaterdc1c4162017-12-12 12:02:04 +00001792 if (val & KVM_XICS_PRESENTED && !(val & KVM_XICS_PENDING))
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001793 state->old_p = true;
1794 if (val & KVM_XICS_QUEUED || val & KVM_XICS_PENDING)
1795 state->old_q = true;
1796
1797 pr_devel(" P=%d, Q=%d\n", state->old_p, state->old_q);
1798
1799 /*
1800 * If the interrupt was unmasked, update guest priority and
1801 * perform the appropriate state transition and do a
1802 * re-trigger if necessary.
1803 */
1804 if (val & KVM_XICS_MASKED) {
1805 pr_devel(" masked, saving prio\n");
1806 state->guest_priority = MASKED;
1807 state->saved_priority = guest_prio;
1808 } else {
1809 pr_devel(" unmasked, restoring to prio %d\n", guest_prio);
1810 xive_finish_unmask(xive, sb, state, guest_prio);
1811 state->saved_priority = guest_prio;
1812 }
1813
1814 /* Increment the number of valid sources and mark this one valid */
1815 if (!state->valid)
1816 xive->src_count++;
1817 state->valid = true;
1818
1819 return 0;
1820}
1821
1822int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1823 bool line_status)
1824{
1825 struct kvmppc_xive *xive = kvm->arch.xive;
1826 struct kvmppc_xive_src_block *sb;
1827 struct kvmppc_xive_irq_state *state;
1828 u16 idx;
1829
1830 if (!xive)
1831 return -ENODEV;
1832
1833 sb = kvmppc_xive_find_source(xive, irq, &idx);
1834 if (!sb)
1835 return -EINVAL;
1836
1837 /* Perform locklessly .... (we need to do some RCUisms here...) */
1838 state = &sb->irq_state[idx];
1839 if (!state->valid)
1840 return -EINVAL;
1841
1842 /* We don't allow a trigger on a passed-through interrupt */
1843 if (state->pt_number)
1844 return -EINVAL;
1845
1846 if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL)
Jiapeng Chongc9df3f82021-02-07 14:43:12 +08001847 state->asserted = true;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001848 else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
Jiapeng Chongc9df3f82021-02-07 14:43:12 +08001849 state->asserted = false;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001850 return 0;
1851 }
1852
1853 /* Trigger the IPI */
1854 xive_irq_trigger(&state->ipi_data);
1855
1856 return 0;
1857}
1858
Greg Kurz062cfab2019-09-27 13:54:01 +02001859int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr)
1860{
1861 u32 __user *ubufp = (u32 __user *) addr;
1862 u32 nr_servers;
1863 int rc = 0;
1864
1865 if (get_user(nr_servers, ubufp))
1866 return -EFAULT;
1867
1868 pr_devel("%s nr_servers=%u\n", __func__, nr_servers);
1869
1870 if (!nr_servers || nr_servers > KVM_MAX_VCPU_ID)
1871 return -EINVAL;
1872
1873 mutex_lock(&xive->lock);
1874 if (xive->vp_base != XIVE_INVALID_VP)
1875 /* The VP block is allocated once and freed when the device
1876 * is released. Better not allow to change its size since its
1877 * used by connect_vcpu to validate vCPU ids are valid (eg,
1878 * setting it back to a higher value could allow connect_vcpu
1879 * to come up with a VP id that goes beyond the VP block, which
1880 * is likely to cause a crash in OPAL).
1881 */
1882 rc = -EBUSY;
1883 else if (nr_servers > KVM_MAX_VCPUS)
1884 /* We don't need more servers. Higher vCPU ids get packed
1885 * down below KVM_MAX_VCPUS by kvmppc_pack_vcpu_id().
1886 */
1887 xive->nr_servers = KVM_MAX_VCPUS;
1888 else
1889 xive->nr_servers = nr_servers;
1890
1891 mutex_unlock(&xive->lock);
1892
1893 return rc;
1894}
1895
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001896static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1897{
1898 struct kvmppc_xive *xive = dev->private;
1899
1900 /* We honor the existing XICS ioctl */
1901 switch (attr->group) {
1902 case KVM_DEV_XICS_GRP_SOURCES:
1903 return xive_set_source(xive, attr->attr, attr->addr);
Greg Kurzefe5ddc2019-09-27 13:54:07 +02001904 case KVM_DEV_XICS_GRP_CTRL:
1905 switch (attr->attr) {
1906 case KVM_DEV_XICS_NR_SERVERS:
1907 return kvmppc_xive_set_nr_servers(xive, attr->addr);
1908 }
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001909 }
1910 return -ENXIO;
1911}
1912
1913static int xive_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1914{
1915 struct kvmppc_xive *xive = dev->private;
1916
1917 /* We honor the existing XICS ioctl */
1918 switch (attr->group) {
1919 case KVM_DEV_XICS_GRP_SOURCES:
1920 return xive_get_source(xive, attr->attr, attr->addr);
1921 }
1922 return -ENXIO;
1923}
1924
1925static int xive_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1926{
1927 /* We honor the same limits as XICS, at least for now */
1928 switch (attr->group) {
1929 case KVM_DEV_XICS_GRP_SOURCES:
1930 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1931 attr->attr < KVMPPC_XICS_NR_IRQS)
1932 return 0;
1933 break;
Greg Kurzefe5ddc2019-09-27 13:54:07 +02001934 case KVM_DEV_XICS_GRP_CTRL:
1935 switch (attr->attr) {
1936 case KVM_DEV_XICS_NR_SERVERS:
1937 return 0;
1938 }
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001939 }
1940 return -ENXIO;
1941}
1942
1943static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd)
1944{
1945 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
1946 xive_native_configure_irq(hw_num, 0, MASKED, 0);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001947}
1948
Cédric Le Goater4131f832019-04-18 12:39:29 +02001949void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001950{
1951 int i;
1952
1953 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1954 struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
1955
1956 if (!state->valid)
1957 continue;
1958
1959 kvmppc_xive_cleanup_irq(state->ipi_number, &state->ipi_data);
Cédric Le Goateref974022019-05-28 14:17:15 +02001960 xive_cleanup_irq_data(&state->ipi_data);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001961 xive_native_free_irq(state->ipi_number);
1962
Cédric Le Goateref974022019-05-28 14:17:15 +02001963 /* Pass-through, cleanup too but keep IRQ hw data */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001964 if (state->pt_number)
1965 kvmppc_xive_cleanup_irq(state->pt_number, state->pt_data);
1966
1967 state->valid = false;
1968 }
1969}
1970
Cédric Le Goater5422e952019-04-18 12:39:42 +02001971/*
Paul Mackerras6f868402019-04-29 11:24:03 +10001972 * Called when device fd is closed. kvm->lock is held.
Cédric Le Goater5422e952019-04-18 12:39:42 +02001973 */
1974static void kvmppc_xive_release(struct kvm_device *dev)
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001975{
1976 struct kvmppc_xive *xive = dev->private;
1977 struct kvm *kvm = xive->kvm;
Cédric Le Goater5422e952019-04-18 12:39:42 +02001978 struct kvm_vcpu *vcpu;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001979 int i;
1980
Cédric Le Goater5422e952019-04-18 12:39:42 +02001981 pr_devel("Releasing xive device\n");
1982
Paul Mackerras6f868402019-04-29 11:24:03 +10001983 /*
Paul Mackerras6f868402019-04-29 11:24:03 +10001984 * Since this is the device release function, we know that
1985 * userspace does not have any open fd referring to the
1986 * device. Therefore there can not be any of the device
1987 * attribute set/get functions being executed concurrently,
1988 * and similarly, the connect_vcpu and set/clr_mapped
1989 * functions also cannot be being executed.
1990 */
Paul Mackerrasc395fe12019-05-23 16:35:07 +10001991
1992 debugfs_remove(xive->dentry);
Paul Mackerras6f868402019-04-29 11:24:03 +10001993
1994 /*
1995 * We should clean up the vCPU interrupt presenters first.
1996 */
1997 kvm_for_each_vcpu(i, vcpu, kvm) {
1998 /*
1999 * Take vcpu->mutex to ensure that no one_reg get/set ioctl
2000 * (i.e. kvmppc_xive_[gs]et_icp) can be done concurrently.
Paul Mackerrasc395fe12019-05-23 16:35:07 +10002001 * Holding the vcpu->mutex also means that the vcpu cannot
2002 * be executing the KVM_RUN ioctl, and therefore it cannot
2003 * be executing the XIVE push or pull code or accessing
2004 * the XIVE MMIO regions.
Paul Mackerras6f868402019-04-29 11:24:03 +10002005 */
2006 mutex_lock(&vcpu->mutex);
2007 kvmppc_xive_cleanup_vcpu(vcpu);
2008 mutex_unlock(&vcpu->mutex);
2009 }
2010
Paul Mackerrasc395fe12019-05-23 16:35:07 +10002011 /*
2012 * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type
2013 * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe
2014 * against xive code getting called during vcpu execution or
2015 * set/get one_reg operations.
2016 */
Paul Mackerras6f868402019-04-29 11:24:03 +10002017 kvm->arch.xive = NULL;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002018
2019 /* Mask and free interrupts */
2020 for (i = 0; i <= xive->max_sbid; i++) {
2021 if (xive->src_blocks[i])
2022 kvmppc_xive_free_sources(xive->src_blocks[i]);
2023 kfree(xive->src_blocks[i]);
2024 xive->src_blocks[i] = NULL;
2025 }
2026
2027 if (xive->vp_base != XIVE_INVALID_VP)
2028 xive_native_free_vp_block(xive->vp_base);
2029
Cédric Le Goater5422e952019-04-18 12:39:42 +02002030 /*
2031 * A reference of the kvmppc_xive pointer is now kept under
2032 * the xive_devices struct of the machine for reuse. It is
2033 * freed when the VM is destroyed for now until we fix all the
2034 * execution paths.
2035 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002036
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002037 kfree(dev);
2038}
2039
Cédric Le Goater5422e952019-04-18 12:39:42 +02002040/*
2041 * When the guest chooses the interrupt mode (XICS legacy or XIVE
2042 * native), the VM will switch of KVM device. The previous device will
2043 * be "released" before the new one is created.
2044 *
2045 * Until we are sure all execution paths are well protected, provide a
2046 * fail safe (transitional) method for device destruction, in which
2047 * the XIVE device pointer is recycled and not directly freed.
2048 */
2049struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type)
2050{
2051 struct kvmppc_xive **kvm_xive_device = type == KVM_DEV_TYPE_XIVE ?
2052 &kvm->arch.xive_devices.native :
2053 &kvm->arch.xive_devices.xics_on_xive;
2054 struct kvmppc_xive *xive = *kvm_xive_device;
2055
2056 if (!xive) {
2057 xive = kzalloc(sizeof(*xive), GFP_KERNEL);
2058 *kvm_xive_device = xive;
2059 } else {
2060 memset(xive, 0, sizeof(*xive));
2061 }
2062
2063 return xive;
2064}
2065
Paul Mackerras6f868402019-04-29 11:24:03 +10002066/*
2067 * Create a XICS device with XIVE backend. kvm->lock is held.
2068 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002069static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
2070{
2071 struct kvmppc_xive *xive;
2072 struct kvm *kvm = dev->kvm;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002073
2074 pr_devel("Creating xive for partition\n");
2075
Greg Kurze7d71c92019-09-27 13:53:38 +02002076 /* Already there ? */
2077 if (kvm->arch.xive)
2078 return -EEXIST;
2079
Cédric Le Goater5422e952019-04-18 12:39:42 +02002080 xive = kvmppc_xive_get_device(kvm, type);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002081 if (!xive)
2082 return -ENOMEM;
2083
2084 dev->private = xive;
2085 xive->dev = dev;
2086 xive->kvm = kvm;
Cédric Le Goater7e10b9a2019-05-24 15:20:30 +02002087 mutex_init(&xive->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002088
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002089 /* We use the default queue size set by the host */
2090 xive->q_order = xive_native_default_eq_shift();
2091 if (xive->q_order < PAGE_SHIFT)
2092 xive->q_page_order = 0;
2093 else
2094 xive->q_page_order = xive->q_order - PAGE_SHIFT;
2095
Greg Kurz062cfab2019-09-27 13:54:01 +02002096 /* VP allocation is delayed to the first call to connect_vcpu */
2097 xive->vp_base = XIVE_INVALID_VP;
2098 /* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
2099 * on a POWER9 system.
2100 */
2101 xive->nr_servers = KVM_MAX_VCPUS;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002102
Benjamin Herrenschmidtbf4159da2018-01-12 13:37:12 +11002103 xive->single_escalation = xive_native_has_single_escalation();
2104
Greg Kurze7d71c92019-09-27 13:53:38 +02002105 kvm->arch.xive = xive;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002106 return 0;
2107}
2108
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02002109int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu)
2110{
2111 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
2112 unsigned int i;
2113
2114 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
2115 struct xive_q *q = &xc->queues[i];
2116 u32 i0, i1, idx;
2117
2118 if (!q->qpage && !xc->esc_virq[i])
2119 continue;
2120
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02002121 if (q->qpage) {
Cédric Le Goater98983672020-12-10 18:14:38 +01002122 seq_printf(m, " q[%d]: ", i);
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02002123 idx = q->idx;
2124 i0 = be32_to_cpup(q->qpage + idx);
2125 idx = (idx + 1) & q->msk;
2126 i1 = be32_to_cpup(q->qpage + idx);
2127 seq_printf(m, "T=%d %08x %08x...\n", q->toggle,
2128 i0, i1);
2129 }
2130 if (xc->esc_virq[i]) {
2131 struct irq_data *d = irq_get_irq_data(xc->esc_virq[i]);
2132 struct xive_irq_data *xd =
2133 irq_data_get_irq_handler_data(d);
2134 u64 pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
2135
Cédric Le Goater98983672020-12-10 18:14:38 +01002136 seq_printf(m, " ESC %d %c%c EOI @%llx",
2137 xc->esc_virq[i],
2138 (pq & XIVE_ESB_VAL_P) ? 'P' : '-',
2139 (pq & XIVE_ESB_VAL_Q) ? 'Q' : '-',
2140 xd->eoi_page);
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02002141 seq_puts(m, "\n");
2142 }
2143 }
2144 return 0;
2145}
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002146
Cédric Le Goater98983672020-12-10 18:14:38 +01002147void kvmppc_xive_debug_show_sources(struct seq_file *m,
2148 struct kvmppc_xive_src_block *sb)
2149{
2150 int i;
2151
2152 seq_puts(m, " LISN HW/CHIP TYPE PQ EISN CPU/PRIO\n");
2153 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
2154 struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
2155 struct xive_irq_data *xd;
2156 u64 pq;
2157 u32 hw_num;
2158
2159 if (!state->valid)
2160 continue;
2161
2162 kvmppc_xive_select_irq(state, &hw_num, &xd);
2163
2164 pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
2165
2166 seq_printf(m, "%08x %08x/%02x", state->number, hw_num,
2167 xd->src_chip);
2168 if (state->lsi)
2169 seq_printf(m, " %cLSI", state->asserted ? '^' : ' ');
2170 else
2171 seq_puts(m, " MSI");
2172
2173 seq_printf(m, " %s %c%c %08x % 4d/%d",
2174 state->ipi_number == hw_num ? "IPI" : " PT",
2175 pq & XIVE_ESB_VAL_P ? 'P' : '-',
2176 pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
2177 state->eisn, state->act_server,
2178 state->act_priority);
2179
2180 seq_puts(m, "\n");
2181 }
2182}
2183
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002184static int xive_debug_show(struct seq_file *m, void *private)
2185{
2186 struct kvmppc_xive *xive = m->private;
2187 struct kvm *kvm = xive->kvm;
2188 struct kvm_vcpu *vcpu;
2189 u64 t_rm_h_xirr = 0;
2190 u64 t_rm_h_ipoll = 0;
2191 u64 t_rm_h_cppr = 0;
2192 u64 t_rm_h_eoi = 0;
2193 u64 t_rm_h_ipi = 0;
2194 u64 t_vm_h_xirr = 0;
2195 u64 t_vm_h_ipoll = 0;
2196 u64 t_vm_h_cppr = 0;
2197 u64 t_vm_h_eoi = 0;
2198 u64 t_vm_h_ipi = 0;
2199 unsigned int i;
2200
2201 if (!kvm)
2202 return 0;
2203
Cédric Le Goater98983672020-12-10 18:14:38 +01002204 seq_puts(m, "=========\nVCPU state\n=========\n");
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002205
2206 kvm_for_each_vcpu(i, vcpu, kvm) {
2207 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
2208
2209 if (!xc)
2210 continue;
2211
Cédric Le Goater98983672020-12-10 18:14:38 +01002212 seq_printf(m, "VCPU %d: VP:%#x/%02x\n"
2213 " CPPR:%#x HWCPPR:%#x MFRR:%#x PEND:%#x h_xirr: R=%lld V=%lld\n",
2214 xc->server_num, xc->vp_id, xc->vp_chip_id,
2215 xc->cppr, xc->hw_cppr,
2216 xc->mfrr, xc->pending,
2217 xc->stat_rm_h_xirr, xc->stat_vm_h_xirr);
Benjamin Herrenschmidtc424c102018-01-12 13:37:11 +11002218
Cédric Le Goatereacc56b2019-04-18 12:39:28 +02002219 kvmppc_xive_debug_show_queues(m, vcpu);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002220
2221 t_rm_h_xirr += xc->stat_rm_h_xirr;
2222 t_rm_h_ipoll += xc->stat_rm_h_ipoll;
2223 t_rm_h_cppr += xc->stat_rm_h_cppr;
2224 t_rm_h_eoi += xc->stat_rm_h_eoi;
2225 t_rm_h_ipi += xc->stat_rm_h_ipi;
2226 t_vm_h_xirr += xc->stat_vm_h_xirr;
2227 t_vm_h_ipoll += xc->stat_vm_h_ipoll;
2228 t_vm_h_cppr += xc->stat_vm_h_cppr;
2229 t_vm_h_eoi += xc->stat_vm_h_eoi;
2230 t_vm_h_ipi += xc->stat_vm_h_ipi;
2231 }
2232
Cédric Le Goater98983672020-12-10 18:14:38 +01002233 seq_puts(m, "Hcalls totals\n");
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002234 seq_printf(m, " H_XIRR R=%10lld V=%10lld\n", t_rm_h_xirr, t_vm_h_xirr);
2235 seq_printf(m, " H_IPOLL R=%10lld V=%10lld\n", t_rm_h_ipoll, t_vm_h_ipoll);
2236 seq_printf(m, " H_CPPR R=%10lld V=%10lld\n", t_rm_h_cppr, t_vm_h_cppr);
2237 seq_printf(m, " H_EOI R=%10lld V=%10lld\n", t_rm_h_eoi, t_vm_h_eoi);
2238 seq_printf(m, " H_IPI R=%10lld V=%10lld\n", t_rm_h_ipi, t_vm_h_ipi);
2239
Cédric Le Goater98983672020-12-10 18:14:38 +01002240 seq_puts(m, "=========\nSources\n=========\n");
2241
2242 for (i = 0; i <= xive->max_sbid; i++) {
2243 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
2244
2245 if (sb) {
2246 arch_spin_lock(&sb->lock);
2247 kvmppc_xive_debug_show_sources(m, sb);
2248 arch_spin_unlock(&sb->lock);
2249 }
2250 }
2251
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002252 return 0;
2253}
2254
Yangtao Li0f6ddf32018-11-05 09:47:17 -05002255DEFINE_SHOW_ATTRIBUTE(xive_debug);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002256
2257static void xive_debugfs_init(struct kvmppc_xive *xive)
2258{
2259 char *name;
2260
2261 name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
2262 if (!name) {
2263 pr_err("%s: no memory for name\n", __func__);
2264 return;
2265 }
2266
2267 xive->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
2268 xive, &xive_debug_fops);
2269
2270 pr_debug("%s: created %s\n", __func__, name);
2271 kfree(name);
2272}
2273
2274static void kvmppc_xive_init(struct kvm_device *dev)
2275{
2276 struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
2277
2278 /* Register some debug interfaces */
2279 xive_debugfs_init(xive);
2280}
2281
2282struct kvm_device_ops kvm_xive_ops = {
2283 .name = "kvm-xive",
2284 .create = kvmppc_xive_create,
2285 .init = kvmppc_xive_init,
Cédric Le Goater5422e952019-04-18 12:39:42 +02002286 .release = kvmppc_xive_release,
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002287 .set_attr = xive_set_attr,
2288 .get_attr = xive_get_attr,
2289 .has_attr = xive_has_attr,
2290};
2291
2292void kvmppc_xive_init_module(void)
2293{
2294 __xive_vm_h_xirr = xive_vm_h_xirr;
2295 __xive_vm_h_ipoll = xive_vm_h_ipoll;
2296 __xive_vm_h_ipi = xive_vm_h_ipi;
2297 __xive_vm_h_cppr = xive_vm_h_cppr;
2298 __xive_vm_h_eoi = xive_vm_h_eoi;
2299}
2300
2301void kvmppc_xive_exit_module(void)
2302{
2303 __xive_vm_h_xirr = NULL;
2304 __xive_vm_h_ipoll = NULL;
2305 __xive_vm_h_ipi = NULL;
2306 __xive_vm_h_cppr = NULL;
2307 __xive_vm_h_eoi = NULL;
2308}