blob: 150be86b1018f823e40b6165f81f10b137676d9e [file] [log] [blame]
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001/*
2 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation.
7 */
8
9/* File to be included by other .c files */
10
11#define XGLUE(a,b) a##b
12#define GLUE(a,b) XGLUE(a,b)
13
14static void GLUE(X_PFX,ack_pending)(struct kvmppc_xive_vcpu *xc)
15{
16 u8 cppr;
17 u16 ack;
18
Benjamin Herrenschmidt2c4fb782017-08-18 12:10:52 +100019 /*
20 * DD1 bug workaround: If PIPR is less favored than CPPR
21 * ignore the interrupt or we might incorrectly lose an IPB
22 * bit.
23 */
24 if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
25 u8 pipr = __x_readb(__x_tima + TM_QW1_OS + TM_PIPR);
26 if (pipr >= xc->hw_cppr)
27 return;
28 }
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100029
30 /* Perform the acknowledge OS to register cycle. */
31 ack = be16_to_cpu(__x_readw(__x_tima + TM_SPC_ACK_OS_REG));
32
33 /* Synchronize subsequent queue accesses */
34 mb();
35
36 /* XXX Check grouping level */
37
38 /* Anything ? */
39 if (!((ack >> 8) & TM_QW1_NSR_EO))
40 return;
41
42 /* Grab CPPR of the most favored pending interrupt */
43 cppr = ack & 0xff;
44 if (cppr < 8)
45 xc->pending |= 1 << cppr;
46
47#ifdef XIVE_RUNTIME_CHECKS
48 /* Check consistency */
49 if (cppr >= xc->hw_cppr)
50 pr_warn("KVM-XIVE: CPU %d odd ack CPPR, got %d at %d\n",
51 smp_processor_id(), cppr, xc->hw_cppr);
52#endif
53
54 /*
55 * Update our image of the HW CPPR. We don't yet modify
56 * xc->cppr, this will be done as we scan for interrupts
57 * in the queues.
58 */
59 xc->hw_cppr = cppr;
60}
61
62static u8 GLUE(X_PFX,esb_load)(struct xive_irq_data *xd, u32 offset)
63{
64 u64 val;
65
66 if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
67 offset |= offset << 4;
68
69 val =__x_readq(__x_eoi_page(xd) + offset);
70#ifdef __LITTLE_ENDIAN__
71 val >>= 64-8;
72#endif
73 return (u8)val;
74}
75
76
77static void GLUE(X_PFX,source_eoi)(u32 hw_irq, struct xive_irq_data *xd)
78{
79 /* If the XIVE supports the new "store EOI facility, use it */
80 if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
Benjamin Herrenschmidt25642702017-06-14 10:19:25 +100081 __x_writeq(0, __x_eoi_page(xd) + XIVE_ESB_STORE_EOI);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100082 else if (hw_irq && xd->flags & XIVE_IRQ_FLAG_EOI_FW) {
83 opal_int_eoi(hw_irq);
84 } else {
85 uint64_t eoi_val;
86
87 /*
88 * Otherwise for EOI, we use the special MMIO that does
89 * a clear of both P and Q and returns the old Q,
90 * except for LSIs where we use the "EOI cycle" special
91 * load.
92 *
93 * This allows us to then do a re-trigger if Q was set
94 * rather than synthetizing an interrupt in software
95 *
96 * For LSIs, using the HW EOI cycle works around a problem
97 * on P9 DD1 PHBs where the other ESB accesses don't work
98 * properly.
99 */
100 if (xd->flags & XIVE_IRQ_FLAG_LSI)
Benjamin Herrenschmidt25642702017-06-14 10:19:25 +1000101 __x_readq(__x_eoi_page(xd) + XIVE_ESB_LOAD_EOI);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000102 else {
103 eoi_val = GLUE(X_PFX,esb_load)(xd, XIVE_ESB_SET_PQ_00);
104
105 /* Re-trigger if needed */
106 if ((eoi_val & 1) && __x_trig_page(xd))
107 __x_writeq(0, __x_trig_page(xd));
108 }
109 }
110}
111
112enum {
113 scan_fetch,
114 scan_poll,
115 scan_eoi,
116};
117
118static u32 GLUE(X_PFX,scan_interrupts)(struct kvmppc_xive_vcpu *xc,
119 u8 pending, int scan_type)
120{
121 u32 hirq = 0;
122 u8 prio = 0xff;
123
124 /* Find highest pending priority */
125 while ((xc->mfrr != 0xff || pending != 0) && hirq == 0) {
126 struct xive_q *q;
127 u32 idx, toggle;
128 __be32 *qpage;
129
130 /*
131 * If pending is 0 this will return 0xff which is what
132 * we want
133 */
134 prio = ffs(pending) - 1;
135
136 /*
137 * If the most favoured prio we found pending is less
138 * favored (or equal) than a pending IPI, we return
139 * the IPI instead.
140 *
141 * Note: If pending was 0 and mfrr is 0xff, we will
142 * not spurriously take an IPI because mfrr cannot
143 * then be smaller than cppr.
144 */
145 if (prio >= xc->mfrr && xc->mfrr < xc->cppr) {
146 prio = xc->mfrr;
147 hirq = XICS_IPI;
148 break;
149 }
150
151 /* Don't scan past the guest cppr */
152 if (prio >= xc->cppr || prio > 7)
153 break;
154
155 /* Grab queue and pointers */
156 q = &xc->queues[prio];
157 idx = q->idx;
158 toggle = q->toggle;
159
160 /*
161 * Snapshot the queue page. The test further down for EOI
162 * must use the same "copy" that was used by __xive_read_eq
163 * since qpage can be set concurrently and we don't want
164 * to miss an EOI.
165 */
166 qpage = READ_ONCE(q->qpage);
167
168skip_ipi:
169 /*
170 * Try to fetch from the queue. Will return 0 for a
171 * non-queueing priority (ie, qpage = 0).
172 */
173 hirq = __xive_read_eq(qpage, q->msk, &idx, &toggle);
174
175 /*
176 * If this was a signal for an MFFR change done by
177 * H_IPI we skip it. Additionally, if we were fetching
178 * we EOI it now, thus re-enabling reception of a new
179 * such signal.
180 *
181 * We also need to do that if prio is 0 and we had no
182 * page for the queue. In this case, we have non-queued
183 * IPI that needs to be EOId.
184 *
185 * This is safe because if we have another pending MFRR
186 * change that wasn't observed above, the Q bit will have
187 * been set and another occurrence of the IPI will trigger.
188 */
189 if (hirq == XICS_IPI || (prio == 0 && !qpage)) {
190 if (scan_type == scan_fetch)
191 GLUE(X_PFX,source_eoi)(xc->vp_ipi,
192 &xc->vp_ipi_data);
193 /* Loop back on same queue with updated idx/toggle */
194#ifdef XIVE_RUNTIME_CHECKS
195 WARN_ON(hirq && hirq != XICS_IPI);
196#endif
197 if (hirq)
198 goto skip_ipi;
199 }
200
201 /* If fetching, update queue pointers */
202 if (scan_type == scan_fetch) {
203 q->idx = idx;
204 q->toggle = toggle;
205 }
206
207 /* Something found, stop searching */
208 if (hirq)
209 break;
210
211 /* Clear the pending bit on the now empty queue */
212 pending &= ~(1 << prio);
213
214 /*
215 * Check if the queue count needs adjusting due to
216 * interrupts being moved away.
217 */
218 if (atomic_read(&q->pending_count)) {
219 int p = atomic_xchg(&q->pending_count, 0);
220 if (p) {
221#ifdef XIVE_RUNTIME_CHECKS
222 WARN_ON(p > atomic_read(&q->count));
223#endif
224 atomic_sub(p, &q->count);
225 }
226 }
227 }
228
229 /* If we are just taking a "peek", do nothing else */
230 if (scan_type == scan_poll)
231 return hirq;
232
233 /* Update the pending bits */
234 xc->pending = pending;
235
236 /*
237 * If this is an EOI that's it, no CPPR adjustment done here,
238 * all we needed was cleanup the stale pending bits and check
239 * if there's anything left.
240 */
241 if (scan_type == scan_eoi)
242 return hirq;
243
244 /*
245 * If we found an interrupt, adjust what the guest CPPR should
246 * be as if we had just fetched that interrupt from HW.
247 */
248 if (hirq)
249 xc->cppr = prio;
250 /*
251 * If it was an IPI the HW CPPR might have been lowered too much
252 * as the HW interrupt we use for IPIs is routed to priority 0.
253 *
254 * We re-sync it here.
255 */
256 if (xc->cppr != xc->hw_cppr) {
257 xc->hw_cppr = xc->cppr;
258 __x_writeb(xc->cppr, __x_tima + TM_QW1_OS + TM_CPPR);
259 }
260
261 return hirq;
262}
263
264X_STATIC unsigned long GLUE(X_PFX,h_xirr)(struct kvm_vcpu *vcpu)
265{
266 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
267 u8 old_cppr;
268 u32 hirq;
269
270 pr_devel("H_XIRR\n");
271
272 xc->GLUE(X_STAT_PFX,h_xirr)++;
273
274 /* First collect pending bits from HW */
275 GLUE(X_PFX,ack_pending)(xc);
276
277 /*
278 * Cleanup the old-style bits if needed (they may have been
279 * set by pull or an escalation interrupts).
280 */
281 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions))
282 clear_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL,
283 &vcpu->arch.pending_exceptions);
284
285 pr_devel(" new pending=0x%02x hw_cppr=%d cppr=%d\n",
286 xc->pending, xc->hw_cppr, xc->cppr);
287
288 /* Grab previous CPPR and reverse map it */
289 old_cppr = xive_prio_to_guest(xc->cppr);
290
291 /* Scan for actual interrupts */
292 hirq = GLUE(X_PFX,scan_interrupts)(xc, xc->pending, scan_fetch);
293
294 pr_devel(" got hirq=0x%x hw_cppr=%d cppr=%d\n",
295 hirq, xc->hw_cppr, xc->cppr);
296
297#ifdef XIVE_RUNTIME_CHECKS
298 /* That should never hit */
299 if (hirq & 0xff000000)
300 pr_warn("XIVE: Weird guest interrupt number 0x%08x\n", hirq);
301#endif
302
303 /*
304 * XXX We could check if the interrupt is masked here and
305 * filter it. If we chose to do so, we would need to do:
306 *
307 * if (masked) {
308 * lock();
309 * if (masked) {
310 * old_Q = true;
311 * hirq = 0;
312 * }
313 * unlock();
314 * }
315 */
316
317 /* Return interrupt and old CPPR in GPR4 */
318 vcpu->arch.gpr[4] = hirq | (old_cppr << 24);
319
320 return H_SUCCESS;
321}
322
323X_STATIC unsigned long GLUE(X_PFX,h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server)
324{
325 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
326 u8 pending = xc->pending;
327 u32 hirq;
328 u8 pipr;
329
330 pr_devel("H_IPOLL(server=%ld)\n", server);
331
332 xc->GLUE(X_STAT_PFX,h_ipoll)++;
333
334 /* Grab the target VCPU if not the current one */
335 if (xc->server_num != server) {
336 vcpu = kvmppc_xive_find_server(vcpu->kvm, server);
337 if (!vcpu)
338 return H_PARAMETER;
339 xc = vcpu->arch.xive_vcpu;
340
341 /* Scan all priorities */
342 pending = 0xff;
343 } else {
344 /* Grab pending interrupt if any */
345 pipr = __x_readb(__x_tima + TM_QW1_OS + TM_PIPR);
346 if (pipr < 8)
347 pending |= 1 << pipr;
348 }
349
350 hirq = GLUE(X_PFX,scan_interrupts)(xc, pending, scan_poll);
351
352 /* Return interrupt and old CPPR in GPR4 */
353 vcpu->arch.gpr[4] = hirq | (xc->cppr << 24);
354
355 return H_SUCCESS;
356}
357
358static void GLUE(X_PFX,push_pending_to_hw)(struct kvmppc_xive_vcpu *xc)
359{
360 u8 pending, prio;
361
362 pending = xc->pending;
363 if (xc->mfrr != 0xff) {
364 if (xc->mfrr < 8)
365 pending |= 1 << xc->mfrr;
366 else
367 pending |= 0x80;
368 }
369 if (!pending)
370 return;
371 prio = ffs(pending) - 1;
372
373 __x_writeb(prio, __x_tima + TM_SPC_SET_OS_PENDING);
374}
375
376X_STATIC int GLUE(X_PFX,h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr)
377{
378 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
379 u8 old_cppr;
380
381 pr_devel("H_CPPR(cppr=%ld)\n", cppr);
382
383 xc->GLUE(X_STAT_PFX,h_cppr)++;
384
385 /* Map CPPR */
386 cppr = xive_prio_from_guest(cppr);
387
388 /* Remember old and update SW state */
389 old_cppr = xc->cppr;
390 xc->cppr = cppr;
391
392 /*
393 * We are masking less, we need to look for pending things
394 * to deliver and set VP pending bits accordingly to trigger
395 * a new interrupt otherwise we might miss MFRR changes for
396 * which we have optimized out sending an IPI signal.
397 */
398 if (cppr > old_cppr)
399 GLUE(X_PFX,push_pending_to_hw)(xc);
400
401 /* Apply new CPPR */
402 xc->hw_cppr = cppr;
403 __x_writeb(cppr, __x_tima + TM_QW1_OS + TM_CPPR);
404
405 return H_SUCCESS;
406}
407
408X_STATIC int GLUE(X_PFX,h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr)
409{
410 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
411 struct kvmppc_xive_src_block *sb;
412 struct kvmppc_xive_irq_state *state;
413 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
414 struct xive_irq_data *xd;
415 u8 new_cppr = xirr >> 24;
416 u32 irq = xirr & 0x00ffffff, hw_num;
417 u16 src;
418 int rc = 0;
419
420 pr_devel("H_EOI(xirr=%08lx)\n", xirr);
421
422 xc->GLUE(X_STAT_PFX,h_eoi)++;
423
424 xc->cppr = xive_prio_from_guest(new_cppr);
425
426 /*
427 * IPIs are synthetized from MFRR and thus don't need
428 * any special EOI handling. The underlying interrupt
429 * used to signal MFRR changes is EOId when fetched from
430 * the queue.
431 */
432 if (irq == XICS_IPI || irq == 0)
433 goto bail;
434
435 /* Find interrupt source */
436 sb = kvmppc_xive_find_source(xive, irq, &src);
437 if (!sb) {
438 pr_devel(" source not found !\n");
439 rc = H_PARAMETER;
440 goto bail;
441 }
442 state = &sb->irq_state[src];
443 kvmppc_xive_select_irq(state, &hw_num, &xd);
444
445 state->in_eoi = true;
446 mb();
447
448again:
449 if (state->guest_priority == MASKED) {
450 arch_spin_lock(&sb->lock);
451 if (state->guest_priority != MASKED) {
452 arch_spin_unlock(&sb->lock);
453 goto again;
454 }
455 pr_devel(" EOI on saved P...\n");
456
457 /* Clear old_p, that will cause unmask to perform an EOI */
458 state->old_p = false;
459
460 arch_spin_unlock(&sb->lock);
461 } else {
462 pr_devel(" EOI on source...\n");
463
464 /* Perform EOI on the source */
465 GLUE(X_PFX,source_eoi)(hw_num, xd);
466
467 /* If it's an emulated LSI, check level and resend */
468 if (state->lsi && state->asserted)
469 __x_writeq(0, __x_trig_page(xd));
470
471 }
472
473 mb();
474 state->in_eoi = false;
475bail:
476
477 /* Re-evaluate pending IRQs and update HW */
478 GLUE(X_PFX,scan_interrupts)(xc, xc->pending, scan_eoi);
479 GLUE(X_PFX,push_pending_to_hw)(xc);
480 pr_devel(" after scan pending=%02x\n", xc->pending);
481
482 /* Apply new CPPR */
483 xc->hw_cppr = xc->cppr;
484 __x_writeb(xc->cppr, __x_tima + TM_QW1_OS + TM_CPPR);
485
486 return rc;
487}
488
489X_STATIC int GLUE(X_PFX,h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
490 unsigned long mfrr)
491{
492 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
493
494 pr_devel("H_IPI(server=%08lx,mfrr=%ld)\n", server, mfrr);
495
496 xc->GLUE(X_STAT_PFX,h_ipi)++;
497
498 /* Find target */
499 vcpu = kvmppc_xive_find_server(vcpu->kvm, server);
500 if (!vcpu)
501 return H_PARAMETER;
502 xc = vcpu->arch.xive_vcpu;
503
504 /* Locklessly write over MFRR */
505 xc->mfrr = mfrr;
506
507 /* Shoot the IPI if most favored than target cppr */
508 if (mfrr < xc->cppr)
509 __x_writeq(0, __x_trig_page(&xc->vp_ipi_data));
510
511 return H_SUCCESS;
512}