2 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation.
9 /* File to be included by other .c files */
11 #define XGLUE(a,b) a##b
12 #define GLUE(a,b) XGLUE(a,b)
14 static void GLUE(X_PFX
,ack_pending
)(struct kvmppc_xive_vcpu
*xc
)
20 * Ensure any previous store to CPPR is ordered vs.
21 * the subsequent loads from PIPR or ACK.
26 * DD1 bug workaround: If PIPR is less favored than CPPR
27 * ignore the interrupt or we might incorrectly lose an IPB
30 if (cpu_has_feature(CPU_FTR_POWER9_DD1
)) {
31 __be64 qw1
= __x_readq(__x_tima
+ TM_QW1_OS
);
32 u8 pipr
= be64_to_cpu(qw1
) & 0xff;
33 if (pipr
>= xc
->hw_cppr
)
37 /* Perform the acknowledge OS to register cycle. */
38 ack
= be16_to_cpu(__x_readw(__x_tima
+ TM_SPC_ACK_OS_REG
));
40 /* Synchronize subsequent queue accesses */
43 /* XXX Check grouping level */
46 if (!((ack
>> 8) & TM_QW1_NSR_EO
))
49 /* Grab CPPR of the most favored pending interrupt */
52 xc
->pending
|= 1 << cppr
;
54 #ifdef XIVE_RUNTIME_CHECKS
55 /* Check consistency */
56 if (cppr
>= xc
->hw_cppr
)
57 pr_warn("KVM-XIVE: CPU %d odd ack CPPR, got %d at %d\n",
58 smp_processor_id(), cppr
, xc
->hw_cppr
);
62 * Update our image of the HW CPPR. We don't yet modify
63 * xc->cppr, this will be done as we scan for interrupts
69 static u8
GLUE(X_PFX
,esb_load
)(struct xive_irq_data
*xd
, u32 offset
)
73 if (xd
->flags
& XIVE_IRQ_FLAG_SHIFT_BUG
)
74 offset
|= offset
<< 4;
76 val
=__x_readq(__x_eoi_page(xd
) + offset
);
77 #ifdef __LITTLE_ENDIAN__
84 static void GLUE(X_PFX
,source_eoi
)(u32 hw_irq
, struct xive_irq_data
*xd
)
86 /* If the XIVE supports the new "store EOI facility, use it */
87 if (xd
->flags
& XIVE_IRQ_FLAG_STORE_EOI
)
88 __x_writeq(0, __x_eoi_page(xd
) + XIVE_ESB_STORE_EOI
);
89 else if (hw_irq
&& xd
->flags
& XIVE_IRQ_FLAG_EOI_FW
) {
95 * Otherwise for EOI, we use the special MMIO that does
96 * a clear of both P and Q and returns the old Q,
97 * except for LSIs where we use the "EOI cycle" special
100 * This allows us to then do a re-trigger if Q was set
101 * rather than synthetizing an interrupt in software
103 * For LSIs, using the HW EOI cycle works around a problem
104 * on P9 DD1 PHBs where the other ESB accesses don't work
107 if (xd
->flags
& XIVE_IRQ_FLAG_LSI
)
108 __x_readq(__x_eoi_page(xd
) + XIVE_ESB_LOAD_EOI
);
110 eoi_val
= GLUE(X_PFX
,esb_load
)(xd
, XIVE_ESB_SET_PQ_00
);
112 /* Re-trigger if needed */
113 if ((eoi_val
& 1) && __x_trig_page(xd
))
114 __x_writeq(0, __x_trig_page(xd
));
125 static u32
GLUE(X_PFX
,scan_interrupts
)(struct kvmppc_xive_vcpu
*xc
,
126 u8 pending
, int scan_type
)
131 /* Find highest pending priority */
132 while ((xc
->mfrr
!= 0xff || pending
!= 0) && hirq
== 0) {
138 * If pending is 0 this will return 0xff which is what
141 prio
= ffs(pending
) - 1;
144 * If the most favoured prio we found pending is less
145 * favored (or equal) than a pending IPI, we return
148 * Note: If pending was 0 and mfrr is 0xff, we will
149 * not spurriously take an IPI because mfrr cannot
150 * then be smaller than cppr.
152 if (prio
>= xc
->mfrr
&& xc
->mfrr
< xc
->cppr
) {
158 /* Don't scan past the guest cppr */
159 if (prio
>= xc
->cppr
|| prio
> 7)
162 /* Grab queue and pointers */
163 q
= &xc
->queues
[prio
];
168 * Snapshot the queue page. The test further down for EOI
169 * must use the same "copy" that was used by __xive_read_eq
170 * since qpage can be set concurrently and we don't want
173 qpage
= READ_ONCE(q
->qpage
);
177 * Try to fetch from the queue. Will return 0 for a
178 * non-queueing priority (ie, qpage = 0).
180 hirq
= __xive_read_eq(qpage
, q
->msk
, &idx
, &toggle
);
183 * If this was a signal for an MFFR change done by
184 * H_IPI we skip it. Additionally, if we were fetching
185 * we EOI it now, thus re-enabling reception of a new
188 * We also need to do that if prio is 0 and we had no
189 * page for the queue. In this case, we have non-queued
190 * IPI that needs to be EOId.
192 * This is safe because if we have another pending MFRR
193 * change that wasn't observed above, the Q bit will have
194 * been set and another occurrence of the IPI will trigger.
196 if (hirq
== XICS_IPI
|| (prio
== 0 && !qpage
)) {
197 if (scan_type
== scan_fetch
)
198 GLUE(X_PFX
,source_eoi
)(xc
->vp_ipi
,
200 /* Loop back on same queue with updated idx/toggle */
201 #ifdef XIVE_RUNTIME_CHECKS
202 WARN_ON(hirq
&& hirq
!= XICS_IPI
);
208 /* If fetching, update queue pointers */
209 if (scan_type
== scan_fetch
) {
214 /* Something found, stop searching */
218 /* Clear the pending bit on the now empty queue */
219 pending
&= ~(1 << prio
);
222 * Check if the queue count needs adjusting due to
223 * interrupts being moved away.
225 if (atomic_read(&q
->pending_count
)) {
226 int p
= atomic_xchg(&q
->pending_count
, 0);
228 #ifdef XIVE_RUNTIME_CHECKS
229 WARN_ON(p
> atomic_read(&q
->count
));
231 atomic_sub(p
, &q
->count
);
236 /* If we are just taking a "peek", do nothing else */
237 if (scan_type
== scan_poll
)
240 /* Update the pending bits */
241 xc
->pending
= pending
;
244 * If this is an EOI that's it, no CPPR adjustment done here,
245 * all we needed was cleanup the stale pending bits and check
246 * if there's anything left.
248 if (scan_type
== scan_eoi
)
252 * If we found an interrupt, adjust what the guest CPPR should
253 * be as if we had just fetched that interrupt from HW.
255 * Note: This can only make xc->cppr smaller as the previous
256 * loop will only exit with hirq != 0 if prio is lower than
257 * the current xc->cppr. Thus we don't need to re-check xc->mfrr
263 * If it was an IPI the HW CPPR might have been lowered too much
264 * as the HW interrupt we use for IPIs is routed to priority 0.
266 * We re-sync it here.
268 if (xc
->cppr
!= xc
->hw_cppr
) {
269 xc
->hw_cppr
= xc
->cppr
;
270 __x_writeb(xc
->cppr
, __x_tima
+ TM_QW1_OS
+ TM_CPPR
);
276 X_STATIC
unsigned long GLUE(X_PFX
,h_xirr
)(struct kvm_vcpu
*vcpu
)
278 struct kvmppc_xive_vcpu
*xc
= vcpu
->arch
.xive_vcpu
;
282 pr_devel("H_XIRR\n");
284 xc
->GLUE(X_STAT_PFX
,h_xirr
)++;
286 /* First collect pending bits from HW */
287 GLUE(X_PFX
,ack_pending
)(xc
);
290 * Cleanup the old-style bits if needed (they may have been
291 * set by pull or an escalation interrupts).
293 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL
, &vcpu
->arch
.pending_exceptions
))
294 clear_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL
,
295 &vcpu
->arch
.pending_exceptions
);
297 pr_devel(" new pending=0x%02x hw_cppr=%d cppr=%d\n",
298 xc
->pending
, xc
->hw_cppr
, xc
->cppr
);
300 /* Grab previous CPPR and reverse map it */
301 old_cppr
= xive_prio_to_guest(xc
->cppr
);
303 /* Scan for actual interrupts */
304 hirq
= GLUE(X_PFX
,scan_interrupts
)(xc
, xc
->pending
, scan_fetch
);
306 pr_devel(" got hirq=0x%x hw_cppr=%d cppr=%d\n",
307 hirq
, xc
->hw_cppr
, xc
->cppr
);
309 #ifdef XIVE_RUNTIME_CHECKS
310 /* That should never hit */
311 if (hirq
& 0xff000000)
312 pr_warn("XIVE: Weird guest interrupt number 0x%08x\n", hirq
);
316 * XXX We could check if the interrupt is masked here and
317 * filter it. If we chose to do so, we would need to do:
329 /* Return interrupt and old CPPR in GPR4 */
330 vcpu
->arch
.gpr
[4] = hirq
| (old_cppr
<< 24);
335 X_STATIC
unsigned long GLUE(X_PFX
,h_ipoll
)(struct kvm_vcpu
*vcpu
, unsigned long server
)
337 struct kvmppc_xive_vcpu
*xc
= vcpu
->arch
.xive_vcpu
;
338 u8 pending
= xc
->pending
;
341 pr_devel("H_IPOLL(server=%ld)\n", server
);
343 xc
->GLUE(X_STAT_PFX
,h_ipoll
)++;
345 /* Grab the target VCPU if not the current one */
346 if (xc
->server_num
!= server
) {
347 vcpu
= kvmppc_xive_find_server(vcpu
->kvm
, server
);
350 xc
= vcpu
->arch
.xive_vcpu
;
352 /* Scan all priorities */
355 /* Grab pending interrupt if any */
356 __be64 qw1
= __x_readq(__x_tima
+ TM_QW1_OS
);
357 u8 pipr
= be64_to_cpu(qw1
) & 0xff;
359 pending
|= 1 << pipr
;
362 hirq
= GLUE(X_PFX
,scan_interrupts
)(xc
, pending
, scan_poll
);
364 /* Return interrupt and old CPPR in GPR4 */
365 vcpu
->arch
.gpr
[4] = hirq
| (xc
->cppr
<< 24);
370 static void GLUE(X_PFX
,push_pending_to_hw
)(struct kvmppc_xive_vcpu
*xc
)
374 pending
= xc
->pending
;
375 if (xc
->mfrr
!= 0xff) {
377 pending
|= 1 << xc
->mfrr
;
383 prio
= ffs(pending
) - 1;
385 __x_writeb(prio
, __x_tima
+ TM_SPC_SET_OS_PENDING
);
388 X_STATIC
int GLUE(X_PFX
,h_cppr
)(struct kvm_vcpu
*vcpu
, unsigned long cppr
)
390 struct kvmppc_xive_vcpu
*xc
= vcpu
->arch
.xive_vcpu
;
393 pr_devel("H_CPPR(cppr=%ld)\n", cppr
);
395 xc
->GLUE(X_STAT_PFX
,h_cppr
)++;
398 cppr
= xive_prio_from_guest(cppr
);
400 /* Remember old and update SW state */
405 * Order the above update of xc->cppr with the subsequent
406 * read of xc->mfrr inside push_pending_to_hw()
411 * We are masking less, we need to look for pending things
412 * to deliver and set VP pending bits accordingly to trigger
413 * a new interrupt otherwise we might miss MFRR changes for
414 * which we have optimized out sending an IPI signal.
417 GLUE(X_PFX
,push_pending_to_hw
)(xc
);
421 __x_writeb(cppr
, __x_tima
+ TM_QW1_OS
+ TM_CPPR
);
426 X_STATIC
int GLUE(X_PFX
,h_eoi
)(struct kvm_vcpu
*vcpu
, unsigned long xirr
)
428 struct kvmppc_xive
*xive
= vcpu
->kvm
->arch
.xive
;
429 struct kvmppc_xive_src_block
*sb
;
430 struct kvmppc_xive_irq_state
*state
;
431 struct kvmppc_xive_vcpu
*xc
= vcpu
->arch
.xive_vcpu
;
432 struct xive_irq_data
*xd
;
433 u8 new_cppr
= xirr
>> 24;
434 u32 irq
= xirr
& 0x00ffffff, hw_num
;
438 pr_devel("H_EOI(xirr=%08lx)\n", xirr
);
440 xc
->GLUE(X_STAT_PFX
,h_eoi
)++;
442 xc
->cppr
= xive_prio_from_guest(new_cppr
);
445 * IPIs are synthetized from MFRR and thus don't need
446 * any special EOI handling. The underlying interrupt
447 * used to signal MFRR changes is EOId when fetched from
450 if (irq
== XICS_IPI
|| irq
== 0) {
452 * This barrier orders the setting of xc->cppr vs.
453 * subsquent test of xc->mfrr done inside
454 * scan_interrupts and push_pending_to_hw
460 /* Find interrupt source */
461 sb
= kvmppc_xive_find_source(xive
, irq
, &src
);
463 pr_devel(" source not found !\n");
469 state
= &sb
->irq_state
[src
];
470 kvmppc_xive_select_irq(state
, &hw_num
, &xd
);
472 state
->in_eoi
= true;
475 * This barrier orders both setting of in_eoi above vs,
476 * subsequent test of guest_priority, and the setting
477 * of xc->cppr vs. subsquent test of xc->mfrr done inside
478 * scan_interrupts and push_pending_to_hw
483 if (state
->guest_priority
== MASKED
) {
484 arch_spin_lock(&sb
->lock
);
485 if (state
->guest_priority
!= MASKED
) {
486 arch_spin_unlock(&sb
->lock
);
489 pr_devel(" EOI on saved P...\n");
491 /* Clear old_p, that will cause unmask to perform an EOI */
492 state
->old_p
= false;
494 arch_spin_unlock(&sb
->lock
);
496 pr_devel(" EOI on source...\n");
498 /* Perform EOI on the source */
499 GLUE(X_PFX
,source_eoi
)(hw_num
, xd
);
501 /* If it's an emulated LSI, check level and resend */
502 if (state
->lsi
&& state
->asserted
)
503 __x_writeq(0, __x_trig_page(xd
));
508 * This barrier orders the above guest_priority check
509 * and spin_lock/unlock with clearing in_eoi below.
511 * It also has to be a full mb() as it must ensure
512 * the MMIOs done in source_eoi() are completed before
513 * state->in_eoi is visible.
516 state
->in_eoi
= false;
519 /* Re-evaluate pending IRQs and update HW */
520 GLUE(X_PFX
,scan_interrupts
)(xc
, xc
->pending
, scan_eoi
);
521 GLUE(X_PFX
,push_pending_to_hw
)(xc
);
522 pr_devel(" after scan pending=%02x\n", xc
->pending
);
525 xc
->hw_cppr
= xc
->cppr
;
526 __x_writeb(xc
->cppr
, __x_tima
+ TM_QW1_OS
+ TM_CPPR
);
531 X_STATIC
int GLUE(X_PFX
,h_ipi
)(struct kvm_vcpu
*vcpu
, unsigned long server
,
534 struct kvmppc_xive_vcpu
*xc
= vcpu
->arch
.xive_vcpu
;
536 pr_devel("H_IPI(server=%08lx,mfrr=%ld)\n", server
, mfrr
);
538 xc
->GLUE(X_STAT_PFX
,h_ipi
)++;
541 vcpu
= kvmppc_xive_find_server(vcpu
->kvm
, server
);
544 xc
= vcpu
->arch
.xive_vcpu
;
546 /* Locklessly write over MFRR */
550 * The load of xc->cppr below and the subsequent MMIO store
551 * to the IPI must happen after the above mfrr update is
552 * globally visible so that:
554 * - Synchronize with another CPU doing an H_EOI or a H_CPPR
555 * updating xc->cppr then reading xc->mfrr.
557 * - The target of the IPI sees the xc->mfrr update
561 /* Shoot the IPI if most favored than target cppr */
563 __x_writeq(0, __x_trig_page(&xc
->vp_ipi_data
));